5

Tôi có 2 ListView trên một mảnh duy nhất và tôi tự hỏi nếu tôi có thể thiết lập cho cả hai cùng một lớp thực hiện AdapterView.OnItemClickListener.AdapterView.OnItemClickListener với nhiều ListView

Ý tôi là, liệu Android nói:

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id) 

Added in API level 1 
Callback method to be invoked when an item in this AdapterView has been clicked. 

Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item. 

Parameters 
parent The AdapterView where the click happened. 
view The view within the AdapterView that was clicked (this will be a view provided by the adapter) 
position The position of the view in the adapter. 
id The row id of the item that was clicked. 

Vì vậy, tôi cho rằng tôi có thể chọn ListView khác nhau mà gọi onClick bởi Chánh (AdapterView nơi nhấp chuột đã xảy ra) ..

Bây giờ làm thế nào tôi có thể xác định ListView ? là có một cách để swicth/trường hợp? hoặc tôi cần phải tạo lớp khác nhau (cũng có thể được vô danh tôi biết) mà thực hiện onItemClickListener và thiết lập để differents ListView differents AdapterView.onItemClickListener?

Trả lời

13

Ok tôi giải quyết:

private class myClass implements AdapterView.OnItemClickListener { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     // TODO Auto-generated method stub 

     switch(parent.getId()) { 
     case R.id.listview1:    
      break; 
     case R.id.listview2: 
      break; 
     } 
    } 
} 
+0

Bulls mắt! Cảm ơn rất nhiều cho câu trả lời. Giải quyết. –

Các vấn đề liên quan