0

各セルに画像とテキスト行の両方が含まれるグリッドビューを使用しています。私がやりたいのは、ユーザーがグリッドビューでそのアイテムを選択したときに画像を変更することです。テキストのない画像だけがあったときにこれを機能させましたが、画像とテキストの両方を含むようにセルを変更すると、エラーが発生します:

java.lang.ClassCastException: android.widget.LinearLayout

選択したアイテムのイメージビューを取得しようとしている行でエラーが発生します。

ImageView ivCheck = (ImageView)parent.getChildAt(position);

私の理解では、これは私が現在行っていることがテキストビューをイメージビューから分離できないためです...しかし、私はこれを行う方法がわかりません。

以下のコード:

grid.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="2"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center" >
</GridView>

icon.xml:

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

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

関連するJavaコード:

gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    //Setting tags, and resetting after failed match
    System.out.println(parent + ", " + v + ", " + position + ", " + id);
    for (int i = 0; i < tiles.length; i++){
        ImageView ivCheck = (ImageView)parent.getChildAt(i); //error here

画像とテキストの両方を使用してグリッドビューを作成するためのチュートリアルをたくさん読みましたが、選択したセルの画像を変更するonclicklistenerを使用したチュートリアルを見つけるのに苦労しました。私は本当に助けに感謝します!

4

1 に答える 1

0

パラメータを使用して、View v変更するビューにアクセスするだけです。

そのようです:

LinearLayout icon = (LinearLayout)v;
ImageView image = icon.findViewById(R.id.icon_image);
// do stuff with image here

ユーザーがこのアイテムから離れると、行った変更は保存されないことに注意してください。これは、GridViewがViewスクロールのために画面から消えたときにそれをリサイクルしようとしているためです。ユーザーがのアイテムを変更できるようにしGridView、スクロールして戻ってきた場合でもアイテムを記憶させたい場合は、Adapterこれに入力するに変更を加える必要がありますGridView

于 2013-03-27T01:29:54.343 に答える