Android ListAdapter 或可能 ListView 在运行时更新文本

发布于 2024-12-07 04:17:51 字数 741 浏览 1 评论 0原文

大家好,我有一个小问题,在网络和这些论坛上找不到合适的答案。请不要引导我阅读人们要求在运行时更改列表视图文本颜色的文章,因为我读了很多文章,但没有找到可以帮助我的文章。

我有一个简单的 ListView,它通过使用 ListAdapter 显示 String 对象数组。

我需要在运行时根据其内容更新一些 ListView 字符串。使用对列表视图创建中使用的列表适配器的全局引用,我可以使用下面的代码获取每个列表视图字符串的内容。

但是,除了检索之外,我希望能够依次修改每个字符串,然后将其放回相同的索引位置,并使列表视图反映更改。如何?

        for (int x = 0; x <= listAdapter.getCount();x++)
        {
            Object o = this.listAdapter.getItem(x);

            if (o.getClass().getSimpleName().equals("String"))
            {
                String s = (String) o;

                  s = modifyString(s);

                //s is the string I want to modify then put back in the same place.

            }//end if
        }//end for

Hello folkes I have this little problem for which I cannot find a suitable answer looking around the web and on these forums. Please don't direct me to articles in which people have requested list view text color changes at run time, as I read lots of them and not found one to help me out.

I have a simple ListView that displays an array of String objects via the use of a ListAdapter.

I need to update some of ListView Strings at run time, based on their contents. Using a global reference to the list adapter used in the lists views creation I can get the contents of each list view String using following code below.

However, in addition to retrieval I'd like to be able to modify each string in turn, then put it back in the same index position and have the list view reflect the changes. How?

        for (int x = 0; x <= listAdapter.getCount();x++)
        {
            Object o = this.listAdapter.getItem(x);

            if (o.getClass().getSimpleName().equals("String"))
            {
                String s = (String) o;

                  s = modifyString(s);

                //s is the string I want to modify then put back in the same place.

            }//end if
        }//end for

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

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

发布评论

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

评论(2

等你爱我 2024-12-14 04:17:51

据我所知,您无法更改适配器中的项目 - 除非您使用自定义适配器(通过扩展 BaseAdapter 等...)

所以,我认为您必须:

  1. 确保适配器的构造函数接受数据结构保存您的字符串
  2. 确保您的数据结构是全局的,
  3. 时就在该数据结构中进行更改
  4. 每当您需要调用 myAdapter.notifyDataSetChanged();

这将告诉适配器列表和列表视图中已完成更改应该重新创建。

更新列表视图后,您甚至可以通过以下方式将用户带回到索引:

list.setSelection(positionWhereTheUserClicked);

我希望这有帮助,如果您需要更多代码参考,请告诉我。

这是一些代码

private ArrayList<String> results = new ArrayList<String>();  //global
private BaseAdapter searchAdapter = new BaseAdapter (results, this);  //global

private void updateResults(final ArrayList<String> updatedList){
    results = updatedList;
    final ListView list = (ListView)findViewById(R.id.search_results);
    list.setAdapter(searchAdapter);
    list.setOnItemClickListener(new ListView.OnItemClickListener(){
        // implementation of what happens when you click on an item //
    });
    searchAdapter.notifyDataSetChanged();
}

这段代码在我这边工作得很好,我希望它有帮助。

As far as I know you cannot change the items in an Adapter - unless you are using a custom Adapter (by extending a BaseAdapter etc...)

So, I think you will have to:

  1. make sure you Adapter's constructor takes in the data structure that holds your strings
  2. make sure your data structure is global
  3. make the changes in that data structure whenever you need to
  4. call myAdapter.notifyDataSetChanged();

This will tell adapter that there were changes done in the list and listview should be recreated.

And after your listview is renewed you can even take the user back to the index by:

list.setSelection(positionWhereTheUserClicked);

I hope this helps, let me know if you need more code references.

Here is some code

private ArrayList<String> results = new ArrayList<String>();  //global
private BaseAdapter searchAdapter = new BaseAdapter (results, this);  //global

private void updateResults(final ArrayList<String> updatedList){
    results = updatedList;
    final ListView list = (ListView)findViewById(R.id.search_results);
    list.setAdapter(searchAdapter);
    list.setOnItemClickListener(new ListView.OnItemClickListener(){
        // implementation of what happens when you click on an item //
    });
    searchAdapter.notifyDataSetChanged();
}

This code works just fine on my end, I hope it helps.

枯叶蝶 2024-12-14 04:17:51

刚刚偶然发现了这个问题并找到了解决方案。
我正在使用

m_ListAdapter = new SimpleAdapter(this, m_List, R.layout.option_list_row, columns, renderTo);

listView 中的每个项目都是一个菜单选项,导致显示一个对话框,一旦通过对话框接收到数据,我所要做的就是使用包含新数据的更新的 ArrayList 创建一个新的 SimpleAdapter ,然后只需将Adapter 设置为新适配器即可。
ListView 将立即更新。

Just stumbled on this problem and found a solution.
I'm using a

m_ListAdapter = new SimpleAdapter(this, m_List, R.layout.option_list_row, columns, renderTo);

Each item in my listView is a manu option causing a dialog to show, once data is received through the dialog, all I have to do is just create a new SimpleAdapter with an updated ArrayList that includes the new data, then just setAdapter to the new adapter.
The ListView will update instantly.

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