实时更新ListView中的TextView

时间:2013-02-28 20:52:52

标签: android android-listview

我在实时更新TextView时遇到问题。我想要使​​用自定义适配器实时更新TextView ListView。我有我的套接字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();
        }
    }    
}


//Here is my solution



 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;
                                        }                                   
                                } 

1 个答案:

答案 0 :(得分: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();