2013-02-28 45 views
4

我在實時更新TextView時遇到問題。我想用一個自定義適配器實時更新ListViewTextView。我有我的套接字I/O處理程序,我收到一條JSON消息。我想分析這個JSON並將該文本放入setText()的特定列表行中。我如何獲得ListView行的索引並更新其TextView實時更新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; 
             }         
           } 

回答

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(); 
+0

讓我試試這一個 – Nik 2013-02-28 21:19:37

+0

@Neik Haarman謝謝 – 2014-10-01 12:21:28