我有一个类适配器,它使用BUCKET_LIST_NAME显示所有文件夹和文件夹的名称。但文件夹的名称相同=照片。如何获得不同的名称,我的意思是如何获得每个文件夹的真实姓名???感谢。
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;
}
答案 0 :(得分:0)
初始化光标:
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
);
将其传递给适配器:
CursorAdapter adapter = ThumbnailAdapter(mContext, cursor);
您的适配器类:
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;
}
}