你试过这个吗
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.ycoin);
否则请提供您的 xml 代码。那么只有我们可以帮助你。
其实我不知道你的问题是什么。我将提供漏洞答案。尝试一下。
我们的 GridActivity 是
public class HomeActivity extends Activity {
static final String[] images = { "Name 1", "Name 2", "Name 3",
"Name 4", "Name 5", "Name 5" };
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actvity_home);
grid = (GridView) findViewById(R.id.gridView1);
grid.setAdapter(new ImageAdapter(this, images));
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String label = ((TextView) arg1.findViewById(R.id.grid_item_label)).getText().toString();
if (label.equals("Name 1")) {
Intent firstActivity = new Intent(HomeActivity.this,
FirstActivity.class);
startActivity(mastersActivity);
} else if (label.equals("Name 2")) {
Intent secondActivity = new Intent(HomeActivity.this,
SecondActivity.class);
startActivity(secondActivity);
} else if (label.equals("Name 3")) {
Intent thirdActivity = new Intent(HomeActivity.this,
thirdActivity.class);
startActivity(thirdActivity);
} else if (label.equals("Name 4")) {
Intent fourthActivity = new Intent(HomeActivity.this,
fourthActivity.class);
startActivity(fourthActivity);
} else if (label.equals("Name 5")) {
Intent secondActivity = new Intent(HomeActivity.this,
fifthActivity.class);
startActivity(fifthActivity);
} else if (label.equals("Name 6")) {
Intent sixthActivity = new Intent(HomeActivity.this,
sixthActivity.class);
startActivity(sixthActivity);
}
}
});
}
}
然后我们的图像适配器类将。
公共类 ImageAdapter 扩展 BaseAdapter { 私有上下文上下文;私有最终字符串 [] 移动值;
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = mobileValues[position];
if (mobile.equals("Name 1")) {
imageView.setImageResource(R.drawable.image1);
} else if (mobile.equals("Name 2")) {
imageView.setImageResource(R.drawable.image2);
} else if (mobile.equals("Name 3")) {
imageView.setImageResource(R.drawable.image3);
} else if (mobile.equals("Name 4")) {
imageView.setImageResource(R.drawable.image4);
}else if (mobile.equals("Name 5")) {
imageView.setImageResource(R.drawable.image5);
}else if (mobile.equals("Name 6")) {
imageView.setImageResource(R.drawable.image6);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
public int getCount() {
return mobileValues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
活动主页.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2">
</GridView>
</LinearLayout>
网格.xml
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="1dp"
android:text="@+id/label"
android:textColor="#ffffff"
android:textSize="15dp" >
</TextView>
试试这个。我希望这能帮到您