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.

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?


Ok I resolved:

private class myClass implements AdapterView.OnItemClickListener {

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

        switch(parent.getId()) {