AdapterView.OnItemClickListener with more ListView

荒凉一梦 提交于 2019-12-05 00:16:31

问题


I have 2 ListView on a single fragment and I wonder if I can set for both same class that implements AdapterView.OnItemClickListener.

I mean, Android Documentation says:

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.

So I suppose that I can choose different ListView that invoked onClick by Parent (AdapterView where click happened)..

Now how can I identify ListView? is there a way to swicth / case? or I need to create different class (can also be anonymous I know) that implements onItemClickListener and set to differents ListView differents AdapterView.onItemClickListener?


回答1:


Ok I resolved:

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;
        }
    }
}


来源:https://stackoverflow.com/questions/17855444/adapterview-onitemclicklistener-with-more-listview