How CursorAdapter works on Android in GridView

I had a problem using the cursor adapter in gridview, which I used with the cursor to load photos from the multimedia store. I realized that my newView and bindView got full permission. I mean, assuming I have 500 photos, newView is also called the same number of times.

Did I do something wrong? I thought he would call only when the cell is visible on the screen.

public int taskA = 0; public GalleryCursorAdapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } @Override public void bindView(View view, Context context, Cursor cursor) { // TODO Auto-generated method stub int index = cursor.getColumnIndex(MediaStore.Images.Media._ID); long id = cursor.getLong(index); Bundle idBundle = new Bundle(); idBundle.putLong("id", id); Message msg = new Message(); msg.setData(idBundle); ImageHandler imgHandler = new ImageHandler(context, (ImageView) view); imgHandler.sendMessage(msg); view.setTag(imgHandler); Log.w("task s", " count"); } @SuppressLint({ "NewApi", "NewApi" }) @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // TODO Auto-generated method stub int index = cursor.getColumnIndex(MediaStore.Images.Media._ID); long id = cursor.getLong(index); ImageView iView = new ImageView(context); Bundle idBundle = new Bundle(); idBundle.putLong("id", id); Message msg = new Message(); msg.setData(idBundle); ImageHandler imgHandler = new ImageHandler(context, iView); imgHandler.sendMessage(msg); iView.setTag(imgHandler); taskA++; Log.w("task s", taskA+ " count"); return iView; } static class ImageHandler extends Handler { private ImageView mView; private Context mContext; public ImageHandler(Context c, ImageView v) { mView = v; mContext = c; } @Override public void handleMessage(Message msg) { Bundle idBundle = msg.getData(); Long id = idBundle.getLong("id"); Bitmap image = MediaStore.Images.Thumbnails.getThumbnail( mContext.getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, new Options()); mView.setImageBitmap(image); } } 
+4
source share
3 answers
 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ImageView iView = new ImageView(context); iView.setLayoutParams(new GridView.LayoutParams(200, 200)); taskA++; Log.w("task s", taskA+ " count"); return iView; } 

Notice, I deleted all the code that should not be in newView (it should be in bindView), replace new GridView.LayoutParams(200, 200) with any height / width you need, do not use a wrapper, since your content is an empty start, in the result is 0x0 pixels, so ALL of the ImageViews from your cursor fit into the GridView right away (so newView and bindView are called for each view)

+7
source

I would just extend the BaseAdapter, not the cursor adapter, and pass the extracted data to the adapter with a callback. However, you are not using any other thread for getThumbnail - the handler runs on the main thread and is intended only to update the user interface.

You should also work with ViewHolders and convertView to speed up Grid-Speed.

I have something like a BaseAdapter for each adapter:

 public abstract class MyBaseAdapter extends BaseAdapter { protected LayoutInflater inflater; protected Context context; public TikBaseAdapter(Context context) { this.context = context; this.inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public final View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); if (convertView == null) { convertView = newView(type, parent); } bindView(position, type, convertView); return convertView; } /** Create a new instance of a view for the specified {@code type}. */ public abstract View newView(int type, ViewGroup parent); /** Bind the data for the specified {@code position} to the {@code view}. */ public abstract void bindView(int position, int type, View view); } 

My real adapter overrides getItemViewType and then uses wiring closets to inflate the correct layout - and works with viewHolders (view.setTag ()) to speed up scroll performance. just use view.getTag () in the bindView method and then edit the View-Items.

+1
source

From what I understand, you need to associate the data with the view you created. Like this:

 public class ExampleCursorAdapter extends CursorAdapter { public ExampleCursorAdapter(Context context, Cursor c) { super(context, c); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView summary = (TextView)view.findViewById(R.id.summary); summary.setText(cursor.getString( cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY))); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.item, parent, false); bindView(v, context, cursor); return v; } } 
0
source

Source: https://habr.com/ru/post/1502351/


All Articles