【发布时间】:2014-03-05 08:50:26
【问题描述】:
我正在为 android 制作带有 Grid View 的应用程序。我在图像下获得了带有文本视图的网格视图代码。但是没有显示文本视图。 Eclipse 没有向我显示任何错误或 logcat 中的任何内容
public class ImageAdapter extends BaseAdapter {
private Context mContext;
TextView text;
private String[] mThumbTxt = {
"Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
"Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
"Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
"Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
"Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
"Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
" Example Text 4","Example 5",
};
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4,
R.drawable.img5, R.drawable.img6,
R.drawable.img7, R.drawable.img20,
R.drawable.img8, R.drawable.img21,
R.drawable.img9, R.drawable.img22,
R.drawable.img10, R.drawable.img23,
R.drawable.img11, R.drawable.img24,
R.drawable.img12, R.drawable.img25,
R.drawable.img13, R.drawable.img26,
R.drawable.img14, R.drawable.img27,
R.drawable.img15, R.drawable.img28,
R.drawable.img16, R.drawable.img29,
R.drawable.img17, R.drawable.img30,
R.drawable.img18, R.drawable.img31,
R.drawable.img19
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
text=new TextView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new GridView.LayoutParams(270, 270));
text.setText(mThumbTxt[position]);
return imageView;
}
这段代码有什么问题?我做错了什么? 单独的 XML 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
【问题讨论】:
标签: android image gridview textview