2011-11-01 99 views
3

我有一個ListView,我需要更新特定行的內容,而無需重新加載所有ListView內容。此ListView可以很大。更新Android中的行內容ListView

例如(爲了簡單起見)我需要更改位於適配器'x'位置的視圖(行)中的TextView(適配器中的label3)的內容。其實我使用TouchListView(Commonsware)適配器。

class IconicAdapter extends ArrayAdapter<FavoriteObject> { 
    IconicAdapter() { 
     super(FavoritesMainActivity.this, R.layout.favbusrow, array); 
    } 

    public View getView(int position, View convertView,ViewGroup parent) { 
     View row=convertView; 
     LayoutInflater inflater=getLayoutInflater(); 

      ListFav lf = (listFav) array.get(position).getContent(); 

      row=inflater.inflate(R.layout.favrow,parent, false); 
      row.setBackgroundColor(Color.WHITE); 

      TextView label=(TextView)row.findViewById(R.id.busNameF); 
      label.setText(lf.getName()); 
      label.setTextColor(Color.BLACK); 
      label.setTypeface(FontsManager.getBoldFont()); 

      TextView label2 =(TextView)row.findViewById(R.id.busNumberF); 
      label2.setText(lf.getNumber()); 
      label2.setTextColor(Color.BLACK); 
      label2.setTypeface(FontsManager.getBoldFont()); 

      TextView label3 =(TextView)row.findViewById(R.id.busDirectionF); 
      label3.setText(lf.getQuickName()); 
      label3.setTextColor(Color.BLACK); 
      label3.setTypeface(FontsManager.getRegularFont()); 

     return(row); 
    } 
} 

有人有想法嗎?

謝謝!

+0

您確實沒有給出足夠的信息以獲得可靠的答案。名單很長嗎?你只更新這一個領域?你使用什麼樣的適配器? –

+0

可以訪問一行的內容嗎? – Maxime

回答

1

我需要更改位於適配器'x'位置的視圖(行)中的TextView(適配器中的label3)的內容。

該位置可能會或可能不在視圖中。您可以使用getFirstVisiblePosition()getLastVisiblePosition()來查看它是否是,然後計算子索引並使用getChildAt()來檢索該行。

您還需要確保您的數據模型反映了此更改,因此,當用戶滾動時,他們不會「丟失」您所做的任何直接到用戶界面的修改。

2

更新適配器中的數據集以反映您的更改,然後在適配器上調用notifyDataSetChanged()。