如何使用CheckedTextView访问ListView?

发布于 2024-11-25 07:50:56 字数 466 浏览 1 评论 0原文

我现在正在开发一个使用 ListView 的应用程序 由 ArrayAdapter 管理的每个项目上的 CheckedTextView 支持多项选择。我的 ListView 中的内容是动态的,即 意味着可以在运行时更改。现在我尝试使用 ListView.getCheckedItemPositions() 获取所有选中的项目,因为我想要 保存所有检查过的位置并在用户返回时自动检查它们 再次这个页面。所以我需要保存每个页面的检查结果。

对于第一页,一切都按预期正常工作。但是当用户去 另一个页面并进行一些选择,ListView返回的结果数组 包含一些从未检查过的位置。我不明白为什么 ListView 有 这种奇怪的行为。即使对于实际上没有检查的页面,但是 ListView 给我一个结果,表明有一个项目已被检查。

谁能教我如何获取 CheckedTextView 的位置 在其 OnClickListener 回调中?

示例代码很感激。

提前致谢...

I'm now developing an application that uses a ListView with a
CheckedTextView on every item that managed by an ArrayAdapter to
support multiple chooses. The contents in my ListView are dynamic, that
means, can be changed during runtime. Now I try to use
ListView.getCheckedItemPositions() to get all checked items, Because I want
to save all the checked positions and auto-check them when user go back to
this page again. So I need to save checked results for every page.

For the first page everything works fine as expected. But when user goes to
another page and make some chooses, the result array that ListView returned
contains some positions that are never checked. I don't why ListView has
this strange behavior. Even for a page that in fact no checked happens but
ListView gives me a result that indicates there's one item has been checked.

could anyone who can teach me how to get the position of CheckedTextView
in its OnClickListener callback?

example code is appreciate.

Thanks in advance...

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

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

发布评论

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

评论(1

魂归处 2024-12-02 07:50:56

列表视图会回收其视图,因此当您转到不同的页面然后返回上一页时,列表视图会调用其视图的 getView() 函数。为了确保检查的视图的顺序不会混淆,请在初始化适配器之前创建一个包含所有视图的检查状态的数组列表。然后将数组列表作为参数传递给适配器的构造函数。在 getView() 函数中,根据数组列表设置每个可检查文本视图的检查状态。然后,返回到活动类并重写 onItemClick() 事件。使用调用函数时提供给您的视图,执行以下操作来获取可检查的文本视图并设置其检查状态:

listView1.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View selectedView, int position , long id)
    {
        CheckedTextView tv = (CheckedTextView)selectedView.findViewById(R.id.textview);
        if (tv.isChecked())
        {
            tv.setChecked(false);
            checkStatesOfViews.get(position) = false;
        }
        else
        {
            tv.setChecked(true);
            checkStatesOfViews.get(position) = true;
        }

}
});

The listview recycles its views so when you go to a different page and then return to the previous page, the listview recalls the getView() function for its views. To make sure that the order of the checked views are not mixed up, create an arraylist that contains the check state of all the views before initializing the adapter. Then pass the arraylist as an argument for the adapter's constructor. There, in the getView() function, set the checked state of each checkable textview based on the arraylist. Then, return to the activity class and override the onItemClick() event. Using the view that is given to you when the function is called, do the following to get the checkable textview and set its checked state:

listView1.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View selectedView, int position , long id)
    {
        CheckedTextView tv = (CheckedTextView)selectedView.findViewById(R.id.textview);
        if (tv.isChecked())
        {
            tv.setChecked(false);
            checkStatesOfViews.get(position) = false;
        }
        else
        {
            tv.setChecked(true);
            checkStatesOfViews.get(position) = true;
        }

}
});

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