我在GridView中用SimpleAdapter的getView()方法加載適配器,每個Item裡有一個ImageView 和一個TextView,還有一個ImageView顯示打勾的圖片,當我在第一個ImageView打勾保存時,滑動在最下面,最下面的Item對自動打勾,並且第一個打勾會消失,
查了一下,好像是出現這種現象的原因,是當最上邊的item移除屏幕後,它的對象會被緩存。而下一個要從底部出來,要顯示的item出現的時候,會判斷其布局(context)與緩存中的是否一樣。一樣的情況下,會把緩存裡的context作為getView的參數傳進來。這樣在畫最後一個item的時候,由於沒有對ImageView做任何改動,所以系統就將第一個的ImageView 內容顯示出來了。現在不知道怎麼改求大神指導下
public class AddActivity extends Activity {
private GridView gridView;
private TextView checkNum;
private List<HashMap<String, Object>> list;
private HashMap<String, Object> hashMap;
private SimpleAdapter adapter;
private String texts[] = null;
private int images[] = null;
private int num=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_activity);
gridView = (GridView) findViewById(R.id.gridview);
checkNum = (TextView) findViewById(R.id.check_num);
list = new ArrayList<HashMap<String,Object>>();
images=new int[]{R.drawable.windy_7, R.drawable.ray_100,
R.drawable.yundong, R.drawable.ganmao,
R.drawable.xiche,R.drawable.liangshai,
R.drawable.fishing};
texts = new String[]{ "風力風向", "紫外線",
"運動提示", "感冒提示",
"洗車提示", "晾曬提示",
"旅游提示"};
for(int i=0;i<images.length;i++){
hashMap=new HashMap<String, Object>();
hashMap.put("imageView",images[i]);
hashMap.put("nameView", texts[i]);
hashMap.put("imageCheck", R.drawable.checkmark_small);
list.add(hashMap);
}
adapter = new SimpleAdapter(this, list, R.layout.gird_activity_item,
new String[]{"imageView","nameView","imageCheck"},
new int[]{R.id.grid_img,R.id.grid_text,R.id.check});
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
ImageView checkImage=(ImageView) view.findViewById(R.id.check);
switch (images[position]) {
case R.drawable.windy_7:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
case R.drawable.ray_100:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
case R.drawable.yundong:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
case R.drawable.ganmao:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
case R.drawable.xiche:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
case R.drawable.liangshai:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
case R.drawable.fishing:
if (checkImage.getVisibility() == View.VISIBLE){
checkImage.setVisibility(View.INVISIBLE);
num--;
checkNum.setText(String.valueOf(num));
}else {
checkImage.setVisibility(View.VISIBLE);
num++;
checkNum.setText(String.valueOf(num));
}
break;
}
}
});
}
}
關鍵的getView中的代碼沒有貼出來