3

TextView在实时更新 a 时遇到问题。我想用自定义适配器实时更新TextViewa 。ListView我有接收 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;
    }
}
4

1 回答 1

8

您应该更改 , 中的项目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();
于 2013-02-28T21:10:18.853 回答