How does CursorAdapter work on android in GridView

后端 未结 3 1338
难免孤独
难免孤独 2021-01-19 13:59

I have a problem with using cursor adapter on gridview which I used the cursor to load photos from the media store. I realized my newView and bindView got called completely.

相关标签:
3条回答
  • 2021-01-19 14:31

    I would simply extend BaseAdapter rather than Cursor Adapter and pass the fetched Data to the Adapter with a callback. Still you are not using any kind of different thread for the getThumbnail - the handler executes in the main thread and is only for updating the UI usually.

    Also you should work with ViewHolders and the convertView to speed up the Grid-Speed.

    I have something like this as BaseAdapter for every 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 using switch-cases to inflate the correct layout - and work with viewHolders (view.setTag()) to speed up the scroll-performance. just use view.getTag() in the bindView method and then edit the View-Items.

    0 讨论(0)
  • 2021-01-19 14:37

    From what I understood, you need to bind data to the View you have 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 讨论(0)
  • 2021-01-19 14:44
    @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;
    }
    

    note, i removed all the code that isn't supposed to be in newView (it should be in bindView) replace new GridView.LayoutParams(200, 200) with whatever height/width you need, don't use wrap content as your content is empty at the beginning, resulting in 0x0 pixels, so ALL of the ImageViews from your cursor fit into the GridView at once (thus newView and bindView get called for every view)

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