irobotxx
irobotxx

Reputation: 6063

Trouble with CursorAdapter and CursorLoader android

Good day, please could somebody help me find out what's wrong with my code. I am probably missing something. Nothing is displayed from my CursorAdapter. Have been struggling with here for some time now. Any help will be highly appreciated!!

public class ManageTagFragment extends Fragment implements LoaderCallbacks<Cursor> {

    private static String TAG = "Image_Debugger";
    private static final int IMAGE_LOADER = 0;
    TextView text;
    GridView grid;
    CustomGridImageAdapter imageAdapter;
    Cursor cursor;
    ImageDB dbadapter;
    private Cursor c;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        dbadapter = new ImageDB(getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.wallpaper_images, container, false);      
        grid = (GridView)view.findViewById(R.id.gridview);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);    
        imageAdapter = new CustomGridImageAdapter(getActivity(), null, 0);
        grid.setAdapter(imageAdapter);
        imageAdapter.notifyDataSetChanged();
        getActivity().getSupportLoaderManager().initLoader(IMAGE_LOADER, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CustomCursorLoader(getActivity().getApplicationContext(), dbadapter, ImageDB.IMAGES);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        imageAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> args) {
        imageAdapter.swapCursor(null);  
    }

    public static final class CustomCursorLoader extends SimpleCursorLoader {

        String columnname;
        ImageDB dbadapter;
        ArrayList<String> imageuri = new ArrayList<String>();

        public CustomCursorLoader(Context context, ImageDB dbadapter, String columnname){
            super(context);
            this.dbadapter = dbadapter;
            this.columnname = columnname;
        }

        public CustomCursorLoader(Context context,String columnname){
            super(context);
            this.columnname = columnname;

        }

        @Override
        public Cursor loadInBackground() {
            Cursor cursor = null;
            dbadapter.open();

            Log.d(TAG, "retrieving Images from database now");
            cursor = dbadapter.retrieveTag(columnname);

            if(cursor.moveToFirst()){
                 final int index = cursor.getColumnIndex(columnname);

                do {
                    final String val = cursor.getString(index); // this is just
                                                                // for test
                    if (val != null) {
                        Log.d(TAG, "files are" + val);
                        imageuri.add(val);
                    }

                } while (cursor.moveToNext());
            }   
            return cursor;
        }
    }
}

CustomGridImageAdapter.java:

public class CustomGridImageAdapter extends CursorAdapter {
    private static String TAG = "Image_Debugger";
    private LayoutInflater inflater;
    private int count;

    public CustomGridImageAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
        inflater = LayoutInflater.from(context);
        Log.d(TAG, "calling grid imageadapter now");
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Log.d(TAG, "calling bind view");
        ViewHolder holder = (ViewHolder) view.getTag();
        String name = cursor.getString(cursor
                .getColumnIndexOrThrow(ImageDB.IMAGES));
        Log.d(TAG, "string name in gridimageadapter is" + name);

        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeFile(name);

        Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
        holder.image.setImageBitmap(newbitmap);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup container) {
        Log.d(TAG, "calling new view here");
        ViewHolder holder = new ViewHolder();
        View view = inflater.inflate(R.layout.media_view, container, false);

        holder.image = (ImageView) view.findViewById(R.id.media_image_id);
        holder.image.setLayoutParams(new GridView.LayoutParams(100, 100));
        holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
        view.setTag(holder);

        return view;
    }

    class ViewHolder {
        ImageView image;
        TextView item_name;
    }
}

Upvotes: 1

Views: 2701

Answers (1)

Andro Selva
Andro Selva

Reputation: 54322

The problem might be here. Its just an assumption. Sorry if I am wrong.

@Override
  public int getCount() {
     return count;
  }

What is the value of count here. I don't find any place where you have passed the value for count.So my assumption is that, the default value for int is returned here (i.e count=0) and hence you don't find any data in your GridView.

Could you check that and comment back.

Upvotes: 1

Related Questions