关于 Android ListView 和 ArrayAdapter 的问题

发布于 2024-11-03 01:33:16 字数 874 浏览 15 评论 0原文

所以我在这里玩弄这段代码,主要目标是单击按钮后用新项目更新列表。我的代码可以工作,但我不确定这是否是正确的方法。

我有两个方法。第一个方法采用我的 ArrayList,添加两个字符串并将其发布到列表视图。伟大的!

当我点击按钮时,会调用我的第二种方法。它添加了一个新字符串并更新了列表,但为了让我更新现有的列表视图,我必须再次执行“setListAdapter(new ArrayAdapter....”行,我不确定这是否正确 我可以使用一些输入,

谢谢!

方法 1:

static final List list = new ArrayList();
private void showEvents (Cursor cursor){
        list.add("foo");
        list.add("bar");
        Log.d(TAG,"showevent");
        setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list));



    }

方法 2(当我点击 listView 下的按钮时调用此方法):

private void updateListView(){
        try{
            list.add("son");
            setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list));


        }catch (Exception e){
            Log.d(TAG, "E="+e);
        }
    }

So I'm toying with this code here, and the main objective is to update a list with a new item once I click a button. The code I have works, but I'm not sure if it's the right way to do it.

I have two methods. The first method takes my ArrayList, add's two strings and posts it to the listview. Great!

The Second method I have is called when I tap on a button. It add's a new string and updates the list, but in order for me to update the existing listview, I had to do the "setListAdapter(new ArrayAdapter...." line again and I'm not sure if that's the right thing to do.

I can use some input please, thanks!

Method 1:

static final List list = new ArrayList();
private void showEvents (Cursor cursor){
        list.add("foo");
        list.add("bar");
        Log.d(TAG,"showevent");
        setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list));



    }

Method 2 (this is called when I tap a button that is under the listView):

private void updateListView(){
        try{
            list.add("son");
            setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list));


        }catch (Exception e){
            Log.d(TAG, "E="+e);
        }
    }

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

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

发布评论

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

评论(2

蘑菇王子 2024-11-10 01:33:16

无需再次设置列表适配器,而是在 ArrayAdapter 上调用 notificationDataSetChanged()。上面的代码将“起作用”,但它会强制整个 ListView 重新绘制,因此如果您有一个很长的列表并且用户已经滚动,它会将它们弹出到顶部。通知是一种更干净的方法。

非常简单的代码。在这种情况下,我在活动中引用了数组适配器:

adapter.notifyDataSetChanged();

这是基于您的代码的示例:

static final List list = new ArrayList();
ArrayAdapter adapter;

private void showEvents (Cursor cursor){
    list.add("foo");
    list.add("bar");
    Log.d(TAG,"showevent");

    adapter = new ArrayAdapter<String>(this, R.layout.singleitem, list)
    setListAdapter(adapter);
}

private void updateListView(){
    try{
        list.add("son");
        adapter.nofityDataSetChanged();
        // not needed anymore
        //setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list));
    }catch (Exception e){
        Log.d(TAG, "E="+e);
    }
}

Rather than setting the list adapter again, call the notifyDataSetChanged() on the ArrayAdapter. The above will "work", but it forces the entire ListView to redraw, so if you have a long list and the user has scrolled it'll pop them back to the top. The notify is a cleaner way to do that.

Very simple code. I've got a reference to the array adapter in the activity in this case:

adapter.notifyDataSetChanged();

Here's and example based on your code:

static final List list = new ArrayList();
ArrayAdapter adapter;

private void showEvents (Cursor cursor){
    list.add("foo");
    list.add("bar");
    Log.d(TAG,"showevent");

    adapter = new ArrayAdapter<String>(this, R.layout.singleitem, list)
    setListAdapter(adapter);
}

private void updateListView(){
    try{
        list.add("son");
        adapter.nofityDataSetChanged();
        // not needed anymore
        //setListAdapter(new ArrayAdapter<String>(this, R.layout.singleitem, list));
    }catch (Exception e){
        Log.d(TAG, "E="+e);
    }
}
过度放纵 2024-11-10 01:33:16

ArrayAdapter 声明一个成员变量(例如 myAdapter),然后在 showEvents 方法中初始化它并将其设置为 listView。将字符串添加到列表后,在 updateListView 方法中,只需调用 myAdapter.notifyDataSetChanged(); 即可解决问题

declare a member variable for the ArrayAdapter (say myAdapter) and then initialise it in the showEvents method and set it to the listView. next in the updateListView method after adding string to your list just call myAdapter.notifyDataSetChanged(); and that should do the trick

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