开发者

How to use more than one list item click that extends Activity implements onClicklistener

开发者_运维技巧

I'm using 3 listviews created in java code in my program. I use extends Activity that implements onitemclicklistener. Now, how to make different item click event for each listviwe separately? Any help is appreciated and thanks in advance.


public void onItemClick(AdapterView<?> adapter, View view, int index, long id)
{ 
    switch(view.getId())
    {
       case <listview1 Id> : //call method 1; break;

       case <listview2 Id> : //call method 2; break;

       case <listview3 Id> : //call method 3; break;

    }
}

This is a bad method.You should implement different classes for listeners.So you can modularize your code.


Don't implement OnItemClickListener in the Activity, instead use separate classes, for instance anonimous classes.

Or use a single listener, and detect the source of the event via the view parameter in the callback method.


if you see

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

It has parent AdapterView. so you can check on which adapter view it is clicked on.

I personally feel not to implement onItemClickListner for this type of instances.

use setOnItemClickListener(listener1) ... and so on for the 3 listViews. it's better.

You can write separate listeners for each of your listView.

HTH.


You need to create a new onItemClickLIstener for each of your ListView's.

You do so like this:

    listView1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int index, long id) {

        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜