2013-03-01 140 views
0

我想實時更新ListView中的TextView。我有一個套接字連接,我正在偵聽json消息,並且我想解析/標識json以更新現有的5個TextView中的一個名單。動態更新TextView

responseid=object.getString("ResponseID"); 
    if(responseid!=null){ 
     for(int j=0;j<myList.size();j++){ 
      //If value match then I want to update the textView of that particular row 
      if(myList.get(j).getName().equals(cid)) { 
        //I successfully get the value of Textview of particular row of Listview now I want to update the this Textview with item variable below. 
       String item = (String) myAdapter.getItem(j).getName(); 
       //Here i want to update Textview 
       myAdapter.notifyDataSetChanged(); 
      } 
     }  
    } 
+0

所以會發生什麼,如果你運行該代碼?它不更新? logcat中有任何錯誤之王嗎? – 2013-03-02 00:08:16

+0

確切地說,你需要什麼? – vgarzom 2013-03-02 00:08:17

+0

您的項目是您定義的對象類型嗎? – abbath 2013-03-02 00:14:01

回答

1
responseid=object.getString("ResponseID"); 
    if(responseid!=null){ 
     for(int j=0;j<myList.size();j++){ 
      if(myList.get(j).getName().equals(cid)) { 
       String item = (String) myAdapter.getItem(j).getName(); 
       ((TextView)myList.get(j)).setText(item); 
       myAdapter.notifyDataSetChanged(); 
      } 
     }  
    } 
+0

myList isrefernce ArrayList myList = new ArrayList (); ithink這不是一個listview,所以我不能使用myList.get(j))。setText(item); – Nik 2013-03-02 02:35:30

+0

我們可能需要更多的信息來幫助你...然後你還沒有給我們太多 – ElefantPhace 2013-03-02 03:58:10

+0

String oldcount = countryList.get(j).getCount(); \t \t \t \t \t \t int oldcountint = Integer。parseInt函數(oldcount); \t \t \t \t \t \t int newcount = oldcountint + 1; \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t countryList.get(j)的.setCount(Integer.toString(newcount)); \t \t \t \t \t \t \t \t \t \t \t \t \t dataAdapter.notifyDataSetChanged(); – Nik 2013-03-02 14:33:36

0

通過致電String item = (String) myAdapter.getItem(j).getName();您只需獲取項目名稱值。

設定值轉換爲getItem到定義的類:

YourObjectType item = (YourObjectType)myAdapter.getItem(j); 
item.setName("updatestring"); 
myAdapter.notifyDataSetChanged(); 
+0

TextView tv =(TextView)dataAdapter.getItem(j)。 \t \t \t \t tv.setText(fffbfbg);像這樣的東西不起作用 – Nik 2013-03-02 01:08:07

+0

什麼是myAdapter的類型? – abbath 2013-03-02 01:21:40

+0

私有類MyCustomAdapter擴展ArrayAdapter Nik 2013-03-02 01:50:53