请告诉我如何在android网格视图教程中更改或应用具有不同文本的不同图标。我的代码是
public class Home extends Activity
{
//---the images to display---
Integer[] imageIDs = {
};
GridView grid_main;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid_main = (GridView) findViewById(R.id.gridview);
grid_main.setAdapter(new ImageAdapter(this));
grid_main.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c)
{
mContext = c;
}
//---returns the number of images---
public int getCount() {
return 12;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text1);
tv.setText("Profile "+position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image1);
iv.setImageResource(R.drawable.bo_1);
}
else
{
v = convertView;
}
return v;
}
}
}
此代码运行正常,但所有图像和文本都相同,所以我需要更改它。请告诉我怎么做? 任何帮助将不胜感激!!
答案 0 :(得分:2)
对于ImageAdapter来说,更好,更好,是:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(mTextsIds[position]);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(mThumbIds[position]);
} else {
v = (View) convertView;
}
return v;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4
};
// references to our texts
private String[] mTextsIds = {
"Text 1", "Text 2", "Text 3", "Text 4"
};
}
请注意,图片和文字的变量为 mThumbIds 和 mTextsIds
grid_item.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal"
>
<ImageView
android:id="@+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColorHighlight="#656565"
/>
</LinearLayout>
答案 1 :(得分:0)
最重要的两条线是这四条:
TextView tv = (TextView)v.findViewById(R.id.icon_text1);
tv.setText("Profile "+position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image1);
if (position == 0) {
iv.setImageResource(R.drawable.bo_1);
} else if (position == 1) {
iv.setImageResource(R.drawable.bo_2);
}
在第一行中,您可以从XMLlayout文件中获取TextView。然后将此TextView的文本设置为“配置文件”和一个数字。在第三行中初始化ImageView。之后,您在该ImageView中使用名为bo1的图像。因此,您所要做的就是在第二行添加另一个文本并更改您在第四行中引用的图像。
答案 2 :(得分:0)
尝试使用以下代码
public View getView(int position, View convertView, ViewGroup parent)
{
View v;
ImageView iv = null;
TextView tv = null;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_item, null);
tv = (TextView)v.findViewById(R.id.icon_text1);
tv.setText("Profile "+position);
iv = (ImageView)v.findViewById(R.id.icon_image1);
}
else
{
v = convertView;
}
iv.setImageResource(imageIDs[position]);
return v;
}
由于 迪帕克