android listview 中的checkbox,Android中ListView与CheckBox的使用,及问题解决
最近自己在編寫有關SIM卡管理的軟件做練習,其中使用到了ListView與CheckBox的的組合,遇到了和大家有同樣的問題:
1.選中一個checkbox對應位置的其他checkbox也會被選中
2.選中一個checkbox之后,滑動滾動條,之前選中的checkbox會莫名其妙的取消選中
于是網上搜之,具體的問題所在,網上答得也是很含糊,由于接觸android時間不長,
其中的原因我也不是很清楚,不過總的來說代碼還是搞定了
寫好的代碼供大家參考下:
import java.util.HashMap;
import java.util.List;
import com.rice.activity.R;
import com.rice.domain.ViewHolder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MyAdapter extends BaseAdapter
{
private Context context;
private
List
Object>> data;
//用來記錄所有ListView記錄對應checkbox的狀態
public HashMap
Boolean> isSelected;
private int resource;
private int to[];
private String from[];
private LayoutInflater inflater = null;
private ViewHolder holder = null;
//構造函數
public MyAdapter(Context context,
List
Object>> data, int resource, String
from[], int to[])
{
this.context = context;
this.data = data;
this.resource = resource;
this.from = new
String[from.length];
this.to = new
int[to.length];
System.arraycopy(from, 0,
this.from, 0, from.length);
System.arraycopy(to, 0,
this.to, 0, to.length);
init();
}
public HashMap
Boolean> getIsSelected()
{
return isSelected;
}
//初始化設置所有checkbox都為未選擇狀態
private void init()
{
isSelected = new
HashMap();
for (int i = 0; i
< data.size(); i++)
{
isSelected.put(i,
false);
}
}
@Override
public int getCount()
{
return data.size();
}
@Override
public Object getItem(int arg0)
{
return data.get(arg0);
}
@Override
public long getItemId(int arg0)
{
return 0;
}
@Override
public View getView(final int position, View
view, ViewGroup arg2)
{
holder = null;
if(null == holder)
{
//總是新建一個ViewHolder對象,用來保存每一個listview條目的信息
holder = new
ViewHolder();
if(null ==
view)
{
inflater
= LayoutInflater.from(context);
view
= inflater.inflate(resource, null);
}
holder.name =
(TextView) view.findViewById(R.id.name);
holder.mobile
= (TextView) view.findViewById(R.id.mobile);
holder.checkBox
= (CheckBox) view.findViewById(R.id.item_checkBox);
}
HashMap
Object> map = data.get(position);
if(null != map)
{
String name =
(String) map.get("name");
String mobile
= (String) map.get("mobile");
holder.name.setText(name);
holder.mobile.setText(mobile);
}
//添加checkBox監聽
holder.checkBox.setOnCheckedChangeListener(new
OnCheckedChangeListener()
{
@Override
public void
onCheckedChanged(CompoundButton arg0, boolean isCheck)
{
if(isCheck)
{
isSelected.put(position,
true);
//System.out.println("add
checked=" + position);
}
else
if(!isCheck)
{
isSelected.put(position,
false);
//System.out.println("remove
checked=" + position);
}
}
});
//根據isSelected中記錄的信息,設置checkbox的狀態
holder.checkBox.setChecked(isSelected.get(position));
return view;
}
}
總結
以上是生活随笔為你收集整理的android listview 中的checkbox,Android中ListView与CheckBox的使用,及问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android恶意代码检测报告,用机器学
- 下一篇: small android,Androi