0

我正在尝试在自定义 gridView 中设置网格的 ImageResource。

ImageView imageView = (ImageView) gridview
                .findViewById(R.id.grid_item_image);
  imageView=(ImageView)gridview.getChildAt(10);
            setImage(imageView);

其中 setImage(img) 是这样定义的方法

     public void setImage(ImageView imageView){
        imageView.setImageResource(R.drawable.ycoin);
    }

但我得到

java.lang.ClassCastException:android.widget.LinearLayout 错误。

GridView 的 xml 部分

   <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"
/> 

    <?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>
4

2 回答 2

1

你试过这个吗

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>

试试这个。我希望这能帮到您

于 2013-03-07T09:22:42.330 回答
1
imageView=(ImageView)gridview.getChildAt(10);

这将返回带有 imageview 和 TextView 的 LinearLayout。当您尝试投射它时,它会抛出ClassCastException.

试试这个

 LinearLayout layout=gridview.getChildAt(10);
imageview=(ImageView)layout.findViewById(R.id.grid_item_image)

我希望这能帮到您。

于 2013-03-07T10:24:54.760 回答