2011-06-01 35 views
0

嗨有一個GridView與我自己的Adapter類。這GridView控件顯示TextViews作爲其項目(簡體):有關GridView上Adpater的設計問題

@Override 
public View getView(int arg0, View convertView, ViewGroup arg2) {   

    final LinearLayout linearLayout;   

    if (convertView == null) { // if it's not recycled, initialize some attributes 

     LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     linearLayout = (LinearLayout)inflater.inflate(R.layout.settings_timer_textview, null); 
     TextView textView = (TextView)linearLayout.findViewById(R.id.timer_textview); 
     textView.setText(text);  

    } else { 
     linearLayout = (LinearLayout) convertView; 
    }   

    return linearLayout; 

} 

如何我我TextView的項目和它的文本的數量可以在運行時改變。這意味着,也許首先我在GridView中有12個TextView,但通過另一個活動中的用戶配置,當用戶返回到GridView活動時,剩下的僅有10個TextView。我如何實現這樣的更新?如果我只是調用notifyDataChanged(),那麼調用getView,但linearLayout!= null,因此文本將不會更新(請參閱上面的代碼 - else分支)。有任何想法嗎?謝謝。

回答

0

你只需將你的文字更新代碼從if語句:

@Override 
public View getView(int arg0, View convertView, ViewGroup arg2) {   

    final LinearLayout linearLayout;   

    if (convertView == null) { // if it's not recycled, initialize some attributes 

     LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     linearLayout = (LinearLayout)inflater.inflate(R.layout.settings_timer_textview, null); 
    } else { 

     linearLayout = (LinearLayout) convertView; 
    } 

    TextView textView = (TextView)linearLayout.findViewById(R.id.timer_textview); 
    textView.setText(text); 

    return linearLayout; 

}