1

i have a class adapter which show me all folders and folder's name by using BUCKET_LIST_NAME. But folders have same name = photo. How to get different names, i mean how to get real name of each folder??? Thanks.

public class ThumbnailAdapter extends BaseAdapter {

// Context required for performing queries
private final Context mContext;

// Cursor for thumbnails
private final Cursor cursor;

private final int count;

String bucket;
String id;


public ThumbnailAdapter(Context c) {
    this.mContext = c;

    // Get list of all images, sorted by last taken first
    final String[] projection = {
            MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME
    };
    String BUCKET_GROUP_BY =
            "1) GROUP BY 1,(2";
    String BUCKET_ORDER_BY = "MAX(datetaken) DESC";

    cursor = mContext.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            BUCKET_GROUP_BY,
            null,
            BUCKET_ORDER_BY
    );
    if (cursor.moveToFirst()) {

        int bucketColumn = cursor.getColumnIndex(
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

        int idColumn = cursor.getColumnIndex(
                MediaStore.Images.Media.BUCKET_ID);


        do {
            // Get the field values
            bucket = cursor.getString(bucketColumn);
            id = cursor.getString(idColumn);


        } while (cursor.moveToNext());
    }

    count = cursor.getCount();
    Log.d("ThumbnailAdapter", count + " images found");
}

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

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

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {

      LinearLayout ll = new LinearLayout(mContext);

      ImageView imageView = new ImageView(mContext);
      TextView mytext = new TextView(mContext);
     mytext.setText(bucket);
      imageView.setImageResource(R.drawable.your_folder_icon);
      ll.addView(imageView);
      ll.addView(mytext);



    return ll;
}
Paltroth
  • 89
  • 1
  • 2
  • 9

1 Answers1

1

Initialize your cursor:

final String[] projection = {
            MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME
    };
    String BUCKET_GROUP_BY =
            "1) GROUP BY 1,(2";
    String BUCKET_ORDER_BY = "MAX(datetaken) DESC";

    cursor = mContext.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            BUCKET_GROUP_BY,
            null,
            BUCKET_ORDER_BY
    );

Pass it to adapter with:

CursorAdapter adapter = ThumbnailAdapter(mContext, cursor);

Your Adapter class:

public class ThumbnailAdapter extends CursorAdapter 
{
    LayoutInflater mInflater;
    Context context;
    Cursor cursor;
    String bucket;
    String id;

    public ThumbnailAdapter(Context context, Cursor c) 
    {
        super(context, c);
        this.context = context;
        this.cursor = c;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) 
    {
        TextView mytext = (TextView) view.findViewById(R.id.mytext);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

         mytext.setText(bucket);
        imageView.setImageResource(R.drawable.your_folder_icon);
    }
    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2)
    {
        View v = mInflater.inflate(R.layout.checklistitem_withbutton, null); // Your XML View.
        return v;
    }
}
Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
  • your code makes same thing, as mine, isn't so??? why i need 2 adapters?can you explain – Paltroth Jun 09 '14 at 07:07
  • your adapter is not taking different items from your array. but my adapter is working on cursor so it will take automatically cursor's value. you should replace your adapter with mine. – Sanket Shah Jun 09 '14 at 07:09
  • sorry, i can't undersand this code should be in 1 class or 2 different?? i should make cursor in Cursoradapter class?? – Paltroth Jun 09 '14 at 07:28
  • no you should initialize cursor in your activity and should pass it in adapter class. – Sanket Shah Jun 09 '14 at 07:34
  • still got error. -.-' have you any small example of such kind of program?? – Paltroth Jun 09 '14 at 07:48
  • http://tausiq.wordpress.com/2012/08/22/android-list-view-from-database-with-cursor-adapter/ and http://chetanandroidarora.wordpress.com/2011/12/18/customcursoradapter/ – Sanket Shah Jun 09 '14 at 07:54
  • 1
    Your answer helped me to find the solution [here](https://stackoverflow.com/a/71005109/17839308), Thank you. – Taha Sami Feb 06 '22 at 07:43