开发者

Design question regarding Adapter on GridView

I have a GridView with my own Adapter class. This GridView displays TextViews as its items (simplified):

@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;
    }     开发者_JS百科   

    return linearLayout;
}

How my the number of my TextView items and its text may change during runtime. That means, maybe at first I have 12 TextViews in my GridView but through the user configuration in another activity, there are only 10 TextViews left when the user returns to the GridView activity. How do I achieve such an update? If I just call notifyDataChanged() then getView is called but linearLayout != null and thus the text will not be updated (see code above - the else branch).


You just need to move your text updating code out of the if statement:

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜