我有一个图像库,我想在每个图像下面显示最后修改的数据,但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>
正如我所说,图像显示,但下面没有文字。
答案 0 :(得分:0)
您将返回ImageView而不是View(布局)。
使用此代码
return v;
答案 1 :(得分:0)
返回
return v;
而不是
return imageView;