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

发布于 2024-12-07 18:32:43 字数 1379 浏览 1 评论 0原文

我是一名 Android 初学者程序员,我想这可能不是最佳实践,但我有一个新闻和类别列表。每个活动都有不同的参数,这些参数会转到另一个活动。我想知道是否有人能如此友善并为此提供解决方法。

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

我怎样才能将每个听众与每个列表相关联?任何帮助将不胜感激。

I'm a beginner programmer in Android and I guess this may not be the best practice but I have a list of news and categories. Each one with different parameters that go to another Activities.I wonder if anyone could be so kind and offer a workaround for this.

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

and the listeners for each one

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

How could I relate each listener with each list?. any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野侃 2024-12-14 18:32:43

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

UPDATE

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

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

When creating your views in the CursorAdapter call view.setTag("List A") or "List B" as appropriate then in your listener method

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

UPDATE

A simpler way is to use the id of the parent control in a switch statement

@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;
       }
    }
}
深者入戏 2024-12-14 18:32:43

我终于成功了,我不知道这是否是最好的方法,但它确实有效。
基本上在“onCreate”上将 setOnItemClickListener 添加到一个列表视图。

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

并与常规侦听器一起使用另一个列表视图。

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

非常感谢梅林提供的所有帮助。

I finally managed to do it, idk if it's the best way but it works.
Basically on the 'onCreate' added the setOnItemClickListener to one 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);

And worked the other Listview with the regular listener.

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

Thanks a lot to Merlin for all the help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文