我正在尝试开发一款应用游戏,其中包含显示 12 张卡片,当您选择 3 张卡片时,这些卡片会被替换。我为此建立了两个类,“位置”和“卡片”。onCreate()
我制作了 72 张卡片并将它们添加到一个ArrayList
并创建 12 个位置,这 12 个位置有一个 int 作为输入,无论是否选择和一张卡片。该卡的输入之一是 , R.drawable.image1
,并且在我的输入中我输入ImageAdapter
了我的mThumbIds
, MainActivity.p1.getCard().getImage()
。p1
是位置的静态实例,所有卡片也是静态的。当一个位置被点击时,它被选中,当其中 3 个被选中时,我有:
if (deck.size() > 0) {
countSel.get(0).setCard(deck.remove(0));
countSel.get(1).setCard(deck.remove(1));
countSel.get(2).setCard(deck.remove(2));
gridview.invalidateViews();
}
其中,deck 包含所有卡片,并且countSel
只有 3 张选定的卡片。所以基本上,现在我的 mThumbIds 中的位置有新卡片和新图片,但即使卡片更新,图像也不会更新。
感谢您的帮助,这是我的其余代码:
图像适配器:
package com.example.set;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public Integer[] getmThumbIds() {
return mThumbIds;
}
public void setmThumbIds(Integer[] mThumbIds) {
this.mThumbIds = mThumbIds;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
notifyDataSetChanged();
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
notifyDataSetChanged();
return imageView;
}
// references to our images
public Integer[] mThumbIds = {
MainActivity.p1.getCard().getImage() , MainActivity.p2.getCard().getImage(),
MainActivity.p3.getCard().getImage(),MainActivity.p4.getCard().getImage() ,
MainActivity.p5.getCard().getImage(), MainActivity.p6.getCard().getImage(),
MainActivity.p7.getCard().getImage() , MainActivity.p8.getCard().getImage(),
MainActivity.p9.getCard().getImage(), MainActivity.p10.getCard().getImage() ,
MainActivity.p11.getCard().getImage(), MainActivity.p12.getCard().getImage(),
MainActivity.p13.getCard().getImage() , MainActivity.p14.getCard().getImage(),
MainActivity.p15.getCard().getImage()
};
}
活动主.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="200dp"
android:numColumns="3"
android:verticalSpacing="60dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
这是我尝试刷新gridview的部分,如果有人认为错误存在,我将发布其余代码,我只是很难正确格式化它:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (position == 0) {
choose(p1);
} else if (position == 1) {
choose(p2);
} else if (position == 2) {
choose(p3);
} else if (position == 3) {
choose(p4);
} else if (position == 4) {
choose(p5);
} else if (position == 5) {
choose(p6);
} else if (position == 6) {
choose(p7);
} else if (position == 7) {
choose(p8);
} else if (position == 8) {
choose(p9);
} else if (position == 9) {
choose(p10);
} else if (position == 10) {
choose(p11);
} else if (position == 11) {
choose(p12);
} else if (position == 12) {
choose(p13);
} else if (position == 13) {
choose(p14);
} else if (position == 14) {
choose(p15);
} else {
}
if (countSel.size() == 3) {
countSel.get(0).setChosen(0);
countSel.get(1).setChosen(0);
countSel.get(2).setChosen(0);
if (set(countSel.get(1), countSel.get(2), countSel.get(0))) {
{
Toast.makeText(MainActivity.this, "Got a Set",
Toast.LENGTH_LONG).show();
}
if (deck.size() > 0) {
countSel.get(0).setCard(deck.remove(0));
countSel.get(1).setCard(deck.remove(1));
countSel.get(2).setCard(deck.remove(2));
} else {
countSel.get(0).setCard(cardnull);
countSel.get(1).setCard(cardnull);
countSel.get(2).setCard(cardnull);
}
} else {
Toast.makeText(MainActivity.this,
"Not A Set" + "remaining" + countSel.size(),
Toast.LENGTH_LONG).show();
}
countSel.clear();
}
{
Toast.makeText(MainActivity.this,
"chicken" + "remaining" +countSel.size(),
Toast.LENGTH_SHORT).show();
}
}
});
gridview.invalidateViews();
}
public void choose(Position p) {
if (p.getChosen() == 0) {
p.setChosen(1);
countSel.add(p);
}
else if (p.getChosen() == 1) {
p.setChosen(0);
countSel.remove(p);
System.out.println(countSel.size());
} else
System.out.println("emo");
}