如何在自定义网格视图中设置子网格的图像资源

时间:2013-03-07 09:18:36

标签: android gridview android-imageview

我正在尝试在自定义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 error。

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>

2 个答案:

答案 0 :(得分: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);
                }
            }
        });         
    }
}

然后我们的图像适配器类将是。

public class ImageAdapter扩展BaseAdapter {     私有上下文;     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>

试试这个。我希望这会对你有所帮助

答案 1 :(得分:1)

imageView=(ImageView)gridview.getChildAt(10);

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

试试这个

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

我希望这会对你有所帮助。