问题
I am trying to set the ImageResource of a grid in customized gridView.
ImageView imageView = (ImageView) gridview
.findViewById(R.id.grid_item_image);
imageView=(ImageView)gridview.getChildAt(10);
setImage(imageView);
where setImage(img) is a method defined like this
public void setImage(ImageView imageView){
imageView.setImageResource(R.drawable.ycoin);
}
but I am getting
java.lang.ClassCastException: android.widget.LinearLayout error.
xml part of GridView
<GridView
android:id="@+id/gridviewmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:columnWidth="0dp"
android:minHeight="30dp"
android:numColumns="6"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
and
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="10px"
android:layout_height="20px"
android:layout_marginRight="0px"
android:src="@drawable/ic_launcher">
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginTop="5px"
android:textColor="#FF00FF"
android:textStyle="bold"
android:textSize="15px" >
</TextView>
</LinearLayout>
回答1:
Did you try this
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.ycoin);
or else please provide your xml code. Then only we can help you.
Actually I don't know what is your problem. I will provide hole answer. Try it.
Our GridActivity is
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);
}
}
});
}
}
then our image adapter class will be.
public class ImageAdapter extends BaseAdapter { private Context context; private final String[] mobileValues;
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;
}
}
activity_home.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>
grid.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>
Try this. I hope this will help you
回答2:
imageView=(ImageView)gridview.getChildAt(10);
This will return LinearLayout with imageview and TextView. When you are trying to Cast it, it throws ClassCastException
.
Try this
LinearLayout layout=gridview.getChildAt(10);
imageview=(ImageView)layout.findViewById(R.id.grid_item_image)
I hope this will help you.
来源:https://stackoverflow.com/questions/15267241/how-can-i-set-the-imageresource-of-a-child-grid-in-customized-gridview