android:如何获取所有带有名称的文件夹

时间:2014-06-09 17:36:44

标签: android image gridview directory mediastore

嗨,有人可以帮我解决我的代码吗?我有一个类应该向我显示我的SD卡+文件夹名称上的文件夹,但所有文件夹都有相同的名称,但每个文件夹应该有不同的名称。我该如何实现呢?

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;
}

1 个答案:

答案 0 :(得分:0)

您可能没有将TextView mytext设置为更改其显示的文字 - 请尝试以下行:

mytext.setText(projection[position]);

我不太确定你是如何索引projection String数组的,但只需提供正确的索引(使用position方法中的getView()变量,你应该是能够正确更改TextView名称。