如何处理1个ListActivity中2个列表视图的点击次数

时间:2011-10-04 16:59:10

标签: android listview handler

我是Android的初学程序员,我想这可能不是最佳做法,但我有一份新闻和类别列表。每一个都有不同的参数去另一个Activities.I想知道是否有人可以这么善良并为此提供解决方法。

nuList = (ListView)findViewById(R.id.list2);
adapter1 = new CategsAdapter(UhoraYCategs.this, names);
nuList.setAdapter(adapter1); 
Utility.setListViewHeightBasedOnChildren(nuList);
list = (ListView)findViewById(android.R.id.list);
adapter = new LazyAdapter(UhoraYCategs.this, imgs, titles, catg);
list.setAdapter(adapter);
Utility.setListViewHeightBasedOnChildren(list);

和每个人的听众

protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(this, UhoraList.class);
    i.putExtra("categoria", names[position]);
    i.putExtra("xml", urls[position]);
    startActivity(i);

}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(this, NewsIntentPave.class);
    i.putExtra("tit", (messages.get(position).getTitle()).toString());
    i.putExtra("descric",(messages.get(position).getDescription()).toString());
    i.putExtra("fecha", (messages.get(position).getDate()).toString());
    i.putExtra("foto", (messages.get(position).getFoto()).toString());
    i.putExtra("url", (messages.get(position).getLink()).toString());
    i.putExtra("banner", "uhora");
    startActivity(i);
}

我如何将每个监听器与每个列表相关联?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

在CursorAdapter调用view.setTag(“List A”)或“List B”中创建视图时,在侦听器方法中

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    if(v.getTag().toString().equals("List A"))
    {
       // do list A stuff
    }
    else
    {
        // do list B stuff
    }
}

<强>更新

更简单的方法是在switch语句中使用父控件的id

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    switch(l.getId())
    {
       case R.id.list:
       {
         // do list A stuff
         break;
       }
       case R.id.list2:
       {
         // do list B stuff
         break;
       }
    }
}

答案 1 :(得分:0)

我终于设法做到了,如果这是最好的方式,但是它有效。 基本上在'onCreate'上将setOnItemClickListener添加到一个Listview中。

nuList = (ListView)findViewById(android.R.id.list);
lazyAdapter = new LazyAdapter(UhoraYCategs.this, imgs, titles, catg);
nuList.setAdapter(lazyAdapter); 

list = (ListView)findViewById(R.id.list2);
adapter = new CategsAdapter(UhoraYCategs.this, names);

list.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> parent, View v, int position, long id){
        Intent i = new Intent(UhoraYCategs.this, UhoraList.class);
        i.putExtra("categoria", names[position]);
        i.putExtra("xml", urls[position]);
        startActivity(i);
    }
}); 
list.setAdapter(adapter);

与其他Listview一起使用常规听众。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(this, NewsIntentPave.class);
    i.putExtra("tit", (messages.get(position).getTitle()).toString());
    i.putExtra("descric", (messages.get(position).getDescription()).toString());
    startActivity(i);
}

非常感谢Merlin的帮助。