-1

i have one error in my ThAdapter class. I want to show images in imageView and name of images in textView but it shows error.I know that error is in getView(), but i don;t know how to change it. Can somebody help me??

public class Th extends BaseAdapter {

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

// Cursor for thumbnails
private final Cursor cursor;
private final int imgId;
private final int imgData;
private final int count;

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

    // Get list of all images, sorted by last taken first
    final String[] projection = {
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA
    };
    cursor = mContext.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            MediaStore.Images.Media.DATE_TAKEN + " DESC"
    );

    // Set constants (column indices and image count)
    imgId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    imgData = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    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) {
    ImageView imageView;
     TextView textView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        textView = new  TextView(mContext);

    } else {
        imageView = (ImageView) convertView;
        textView = (TextView) convertView;
    }

    // Move cursor to image position, fetch id, and generate/view thumbnail
    cursor.moveToPosition(position);
    final Bitmap thumbnail = MediaStore.Images.Thumbnails.getThumbnail(
            mContext.getContentResolver(),
            cursor.getInt(imgId), 
            MediaStore.Images.Thumbnails.MICRO_KIND,
            null
    );

    imageView.setImageBitmap(thumbnail);
     textView.setText(cursor.getString(imgData));


    return imageView;
}

/**
 * Get the image path from the given position
 * @param position
 * @return
 */
public String getImagePath(int position) {
    cursor.moveToPosition(position);
    return cursor.getString(imgData);
}
Paltroth
  • 89
  • 1
  • 2
  • 9

1 Answers1

0

Your getView is returning only an image view. So the textview isn't used. You want to return a layout (Linear or Relative) with both views inside it

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127