在Android中为图库的每个图像项添加文本

在Android中为图库的每个图像项添加文本,android,android-gallery,Android,Android Gallery,我有一个图像库,我想在每个图像下面显示上次修改的数据,但在TextView中没有显示任何内容,但在ImageView中显示“是”。这是我的适配器 public class ImageAdapter extends BaseAdapter implements SpinnerAdapter { private Context context; private Drawable[] pics; private String[] filePaths; public

我有一个图像库,我想在每个图像下面显示上次修改的数据,但在TextView中没有显示任何内容,但在ImageView中显示“是”。这是我的适配器

public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {

    private Context context;
    private Drawable[] pics;
    private String[] filePaths;

    public ImageAdapter(Context context, Drawable[] pics, String[] filePaths) {
        this.context=context;
        this.pics =pics;
        this.filePaths=filePaths;
    }

    @Override
    public int getCount() {
        return pics.length;
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=view;
        ImageView imageView;
        TextView textView;


        if(v==null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.gallery_item, null);
        }

        imageView=(ImageView)v.findViewById(R.id.galleryImageView);
        textView=(TextView)v.findViewById(R.id.galleryTextView);

        imageView.setLayoutParams(new Gallery.LayoutParams(115, 100));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8,8,8,8);

        File f=new File(filePaths[i]);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy");
        Date lastModified=new Date(f.lastModified());

        imageView.setImageDrawable(pics[i]);
        textView.setText(simpleDateFormat.format(lastModified));

        return imageView;
    }

}
这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/galleryImageView" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/galleryTextView"
        android:textColor="#000"/>
</LinearLayout>

正如我所说,图像显示出来了,但下面没有文本。

您返回的是ImageView而不是ViewLayout

使用此代码

return v;
返回

return v;
而不是

return imageView;

尝试吐司或记录上次修改。可能你已经空了。我有一个记录显示日期的日志,它是ok@Doruko你必须返回视图而不是图像视图…该死,我没有注意到。。。谢谢