1

当我收到以下错误时,如何在我的特定网格视图中更改图像。我在其他地方也遵循了类似的例子,但没有成功。即在尝试以下操作时,我得到“无法从整数更改为图像视图”,从(参见Android GridView - update View based on position),即:

int tempid = v.getId();
ImageView imgView = FruitToChooseFromImages.get(tempid);
imgView.setBackgroundResource(R.drawable.blank);

我也试过:

{   // remove viewable image / make blank
ImageView imageView = (ImageView) v;
imageView.setImageResource(R.drawable.blank);
}

如果您需要,下面的完整代码:

主要JAVA代码:

public class game1mainscreen extends Activity {

private MyGridviewAdapter1 mAdapter; 
private GridView gridview;  

private ArrayList<Integer> FruitToChooseFromImages; 
private ArrayList<Integer> tempintarray;

int Totalfruit = 11; // starting from 0 of course (so an 11=12 fruit) this means there are 6 pairs of fruit to select
int fruitleft = 12;
int fruitstilltoadd = Totalfruit + 1;
int numberOfFruitToRemove = 6;   
int puthere = 0; 
int imagesSelectedThusFar = 0;
int Fruitleft = 0;


int firstfruitselected = R.drawable.blank; int firstfruitselpos;
int secondfruitselected = R.drawable.blank; int secondfruitselpos;

int removefirstimage = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameonemenu);


    {        
        FruitToChooseFromImages = new ArrayList<Integer>();

        FruitToChooseFromImages.add(R.drawable.fruit0);
        .....etc....
        FruitToChooseFromImages.add(R.drawable.fruit11);

        }

    ...code removed which randomly sorts FruitToChooseFromImages (ie arraylist of images) into 6 duplicate pairs of images...


    // prepared arraylist is passed to the Adapter class  
    mAdapter = new MyGridviewAdapter1(this, FruitToChooseFromImages);  

    // Set custom adapter to gridview  
    gridview = (GridView) findViewById(R.id.gridView1);
    gridview.setAdapter(mAdapter);

    // DO THIS WHEN CLICKED
    gridview.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        { 
            int fruitselected = (int)FruitToChooseFromImages.get(position);     
            // start
            if (fruitselected == R.drawable.blank)
            {  
            // if blank/empty icon clicked on then DO NOTHING
            }

            else 
            { // firstly, say fruit name
                int soundtoplaynow = getrightsound(position, fruitselected);
                SoundManager.playSound(soundtoplaynow, 1);
              // next
                if (imagesSelectedThusFar == 0) 
                    {
                    imagesSelectedThusFar = 1;
                    firstfruitselected = fruitselected; firstfruitselpos = position;
                    //both switch fruit image to blank in the array and display blank at position clicked
                    FruitToChooseFromImages.set(position, R.drawable.blank);
                        {   // remove viewable image / make blank
                        ImageView imageView = (ImageView) v;
                        imageView.setImageResource(R.drawable.blank);
                        }
                    }
                else // so two images have been selected! do they match?
                    {
                    imagesSelectedThusFar = 0; // has reached 2, so reset to zero
                    secondfruitselected = fruitselected; secondfruitselpos = position;
                    if (firstfruitselected == secondfruitselected)
                        {
                    GeneralSoundManager.playSound(0, 1);// play happy sound - change code to GeneralSoundManager

                        //switch fruit image to blank and display blank at position clicked **
                        FruitToChooseFromImages.set (position, R.drawable.blank);
                    {   // remove viewable image / make blank
                        ImageView imageView = (ImageView) v;
                        imageView.setImageResource(R.drawable.blank);
                        }

                        int tempid = v.getId();
                        ImageView imgView = FruitToChooseFromImages.get(tempid);
                        imgView.setBackgroundResource(R.drawable.blank);


                        fruitleft = (fruitleft - 2);
                        }

                    else
                        { // you selected the wrong fruit, sorry.
                    GeneralSoundManager.playSound(1, 1); 
                        FruitToChooseFromImages.set (firstfruitselpos, firstfruitselected);
                        // 
                        }   
                    }
            }    

             // end

            if (fruitleft == 0)
                {
                //end game - add code here
                }
        }
    });
}



// returns the sound to play depending on what image was clicked on
public int getrightsound(Integer position, Integer switchused) {
...etc.... 
    return soundtoplay;
}

}

我的gridview适配器代码:

public class MyGridviewAdapter1 extends BaseAdapter  
{  
private ArrayList<Integer> FruitToChooseFromImages;

private Activity activity;  

public MyGridviewAdapter1(Activity activity,ArrayList<Integer> FruitToChooseFromImages) 
{  
    super();  

    this.FruitToChooseFromImages = FruitToChooseFromImages;  
    this.activity = activity;  
}  


public int getCount() {  
    return FruitToChooseFromImages.size();  
}  


public Integer getItem(int position) {  
    return FruitToChooseFromImages.get(position);  
}  


public long getItemId(int position) {  
    return 0;  
}  

public static class ViewHolder  
{  
    public ImageView imgViewFlag;  

}  


public View getView(int position, View convertView, ViewGroup parent) {   
    ViewHolder view;  
    LayoutInflater inflator = activity.getLayoutInflater();  

    if(convertView==null)  
    {  
        view = new ViewHolder();  
        convertView = inflator.inflate(R.layout.gridview_row, null);  


        view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);  

        convertView.setTag(view);  
    }  
    else  
    {  
        view = (ViewHolder) convertView.getTag();  
    }  


    view.imgViewFlag.setImageResource(FruitToChooseFromImages.get(position));  

    return convertView;  
}  
}  

我的主要 xml 代码 - gameonemenu:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_gravity="center"
android:background="@drawable/tree">
<GridView  

android:id="@+id/gridView1"  
android:layout_width="fill_parent"     
android:layout_height="wrap_content"
android:numColumns="4"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>  

我的 Gridview_row xml 代码:

<ImageView  

    android:layout_height="64dp"  
    android:id="@+id/imageView1"  
    android:layout_width="64dp"  
    android:src="@drawable/blank"  
    android:layout_alignParentTop="true"  
    android:layout_centerHorizontal="true">  
</ImageView>  


</RelativeLayout>

PS 我使用了最初显示文本的 Grirdview_row xml,但我不需要仅显示文本图像,但这会导致问题吗?PSS 请将您的建议保持在 COMPLETE 初学者级别,并尽可能包含代码以匹配解释。我不能强调这一点。谢谢。

4

0 回答 0