Android ListAdapter 或可能 ListView 在运行时更新文本
大家好,我有一个小问题,在网络和这些论坛上找不到合适的答案。请不要引导我阅读人们要求在运行时更改列表视图文本颜色的文章,因为我读了很多文章,但没有找到可以帮助我的文章。
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您无法更改适配器中的项目 - 除非您使用自定义适配器(通过扩展 BaseAdapter 等...)
所以,我认为您必须:
这将告诉适配器列表和列表视图中已完成更改应该重新创建。
更新列表视图后,您甚至可以通过以下方式将用户带回到索引:
我希望这有帮助,如果您需要更多代码参考,请告诉我。
这是一些代码
这段代码在我这边工作得很好,我希望它有帮助。
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:
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:
I hope this helps, let me know if you need more code references.
Here is some code
This code works just fine on my end, I hope it helps.
刚刚偶然发现了这个问题并找到了解决方案。
我正在使用
listView 中的每个项目都是一个菜单选项,导致显示一个对话框,一旦通过对话框接收到数据,我所要做的就是使用包含新数据的更新的 ArrayList 创建一个新的 SimpleAdapter ,然后只需将Adapter 设置为新适配器即可。
ListView 将立即更新。
Just stumbled on this problem and found a solution.
I'm using a
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.