gpt4 book ai didi

android - 实时更新ListView中的TextView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:27 25 4
gpt4 key购买 nike

我在实时更新 TextView 时遇到问题。我想用自定义适配器实时更新 ListViewTextView。我有接收 JSON 消息的套接字 I/O 处理程序。我想解析这个 JSON 并使用 setText() 将该文本放入特定的列表行。如何获取该 ListView 行的索引并更新其 TextView

responseid=object.getString("ResponseID");
if(responseid!=null){
for(int j=0;j<countryList.size();j++){
//If the countryList row match then i want to update the textView of that particular row
if(countryList.get(j).getName().equals(caseid)) {
int oldcount = Integer.parseInt((dataAdapter.findViewById(R.id.replycount)).getText().toString());
int total=oldcount+1;
(dataAdapter.findViewById(R.id.replycount)).setText(Integer.toString(total));
dataAdapter.notifyDataSetChanged();
}
}
}

这是我的解决方案:

for(int j=0;j<countryList.size();j++)
{
if(countryList.get(j).getName().equals(caseid))
{
String oldcount = countryList.get(j).getCount();
int oldcountint = Integer.parseInt(oldcount);
int newcount = oldcountint + 1;
countryList.get(j).setCount(Integer.toString(newcount));
dataAdapter.notifyDataSetChanged();
break;
}
}

最佳答案

您应该更改 ListAdapter 中的项目,然后调用 notifyDataSetChanged()

例子:

//Creating and adding ListAdapter to ListView
MyObject myFirstObject = new MyObject("Test1");
MyObject mySecondObject = new MyObject("Test2");

MyAdapter myAdapter = new MyAdapter();
myAdapter.add(myFirstObject);
myAdapter.add(mySecondObject);
myListView.setAdapter(myAdapter);

更新特定位置时:

myAdapter.getItem(position).text = "My updated text";
myAdapter.notifyDataSetChanged();

关于android - 实时更新ListView中的TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com