How to refresh a GridView?

后端 未结 9 2207
清歌不尽
清歌不尽 2020-12-01 08:01

I have a GridView which is pretty similar to the Google tutorial, except that I want to add the ImageViews on runtime (via a subactivity). The results are okay, but the layo

相关标签:
9条回答
  • 2020-12-01 08:24
    adapter.notifyDataChanged();
    

    may not works just because data for the adapter is stale (if Activity or fragment was not destroyed and have been sitting in the back stack for instance).

    So, if firstly refresh data, then after it will be working.

    0 讨论(0)
  • 2020-12-01 08:28

    You should not call invalidateViews and setAdapter to refresh your grid view. This is not a good idea to keep your grid view updated, if you update in that way it would cost a lot of time and memory.

    If you have a chance to look at getView method you will see that convertView is created just once. When you call notifyDataChanged, it will update this view. Whereas if you call invalidateViews, previously created views will be recreated. This is not a good solution.

    Your getView method is called when you call notifyDataChanged. So your getView method should look something like the code below.

    public List list;
    
    public class SimpleItemViewHolder extends Object
    {
        public TextView textView;
        public ImageView imageView;
    }
    
    public View getView(int position, View convertView, ViewGroup parent){
    
        View itemView = convertView;
        SimpleItemViewHolder viewHolder;
    
        if(convertView==null)
        {
            viewHolder = (SimpleItemViewHolder)itemView.getTag();
    
        }else{
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.layout_name, null);
            TextView labelField = (TextView) itemView.findViewById(R.id.label_field);
            labelField.setText(list.get(position).Name);
            //Typeface boldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Font-Bold.ttf");
            //labelField.setTypeface(boldFont);
    
            ImageView imageView = (ImageView) itemView.findViewById(R.id.image_view);
            //Bitmap bitmap = init your bitmap here;
            //imageView.setImageBitmap(bitmap);  
    
            viewHolder = new SimpleItemViewHolder();
            viewHolder.imageView = imageView;
            viewHolder.textView = labelField;
            itemView.setTag(viewHolder);
    
        }
    
        //don't create new views, instead use previous ones and update them.
        viewHolder.textView.setText(list.get(position).Name);
        //viewHolder.imageView.setImageBitmap(your_bitmap);  
        return itemView;
    }
    
    0 讨论(0)
  • 2020-12-01 08:32

    None of these answers actually worked for me and I had to mash all of them together. To actually get the GridView to update, you need to do this:

     adapter.notifyDataChanged();
     grid.invalidateViews();
     grid.setAdapter(adapter);
    

    Hope this helps anyone who couldn't get the other solutions to work.

    0 讨论(0)
提交回复
热议问题