实时更新ListView中的TextView

Nik*_*Nik 4 android android-listview

TextView在实时更新时遇到问题.我想更新TextViewListView实时与自定义适配器.我有我的套接字I/O处理程序,我收到一个JSON消息.我想解析这个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();
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

nha*_*man 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);
Run Code Online (Sandbox Code Playgroud)

更新特定职位时:

myAdapter.getItem(position).text = "My updated text";
myAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)