我有一个GridView of Images,下面是与该图像相关的文本,我将它放在一个字符串数组中。现在,当我尝试在gridview中的图像下面打印数组的内容时,它会在所有图像中显示该字符串数组的字符串的完整列表。 例如 我的字符串数组如下:
public static String[] gname =
{"ImageA", "ImageB","ImageC", "ImageD","ImageE"
};
我的输出在gridView图像下方为
"ImageA,ImageB,ImageC,ImageD,ImageE"
在网格中的每个图像中。
我希望ImageA在一个图像下显示,ImageB在另一个图像中显示。这怎么可能。哪里错了。请通过查看以下代码告诉我:
MainActivity.java
package com.example.imageandtext;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
gridView.setAdapter(customGridAdapter);
}
public static String[] gname = {
"ImageA", "ImageB",
"ImageC", "ImageD",
"ImageE"
};
private ArrayList getData() {
final ArrayList imageItems = new ArrayList();
// retrieve String drawable array
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
imgs.getResourceId(i, -1));
// Arrays.toString(gname) is somewhat wrong here.. how to correct it?
imageItems.add(new ImageItem(bitmap, Arrays.toString(gname)));
}
return imageItems;
}
}
答案 0 :(得分:0)
尝试:
imageItems.add(new ImageItem(bitmap,gname [i]));