大家好日子。
我有一个列表视图,每个文本视图附近都有复选框。当我勾选一个复选框并向下滚动时,也会检查随机的其他复选框。我在这里读过这个post和其他几个,他们都说每个复选框的状态需要保存在getView()
的布尔列表中。我该怎么做呢?
感谢。
编辑代码:
这是适配器的代码,也包含getView()
方法。
public class MyCustomAdapter extends BaseAdapter
{
public ArrayList<Error> list;
private Context mContext;
private LayoutInflater mInflator;
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<Error> list)
{
this.list = list;
this.mContext = context;
mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Error getItem(int position)
{
return list.get(position);
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = mInflator.inflate(R.layout.listviewrow, null);
holder.text = (TextView)convertView.findViewById(R.id.text);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
final int pos = position;
holder.text.setText(list.get(position).getDescription());
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
list.get(pos).setChecked(isChecked);
}
});
return convertView;
}
static class ViewHolder
{
TextView text;
CheckBox checkbox;
}
}
答案 0 :(得分:9)
下面代码为我工作
我的主要活动
public class MainActivity extends Activity {
private ListView llChb;
private String[] data = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z" };
private ArrayList<String> arrData=null;
private ArrayList<InfoRowdata> infodata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrData=new ArrayList<String>();
arrData.add("a");
arrData.add("b");
arrData.add("c");
arrData.add("d");
arrData.add("e");
arrData.add("f");
arrData.add("g");
arrData.add("h");
arrData.add("i");
arrData.add("j");
arrData.add("k");
arrData.add("l");
arrData.add("m");
arrData.add("n");
arrData.add("o");
arrData.add("p");
llChb = (ListView) findViewById(R.id.llChb);
infodata = new ArrayList<InfoRowdata>();
for (int i = 0; i < data.length; i++) {
infodata.add(new InfoRowdata(false, i));
// System.out.println(i);
//System.out.println("Data is == "+data[i]);
}
llChb.invalidate();
llChb.setAdapter(new MyAdapter());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = null;
row = View.inflate(getApplicationContext(), R.layout.row, null);
TextView tvContent=(TextView) row.findViewById(R.id.tvContent);
//tvContent.setText(data[position]);
tvContent.setText(data[position]);
//System.out.println("The Text is here like.. == "+tvContent.getText().toString());
final CheckBox cb = (CheckBox) row
.findViewById(R.id.chbContent);
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (infodata.get(position).isclicked) {
infodata.get(position).isclicked = false;
} else {
infodata.get(position).isclicked = true;
}
for(int i=0;i<infodata.size();i++)
{
if (infodata.get(i).isclicked)
{
System.out.println("Selectes Are == "+ data[i]);
}
}
}
});
if (infodata.get(position).isclicked) {
cb.setChecked(true);
}
else {
cb.setChecked(false);
}
return row;
}
}
}
我的InfoRowdata模型类
public class InfoRowdata {
public boolean isclicked=false;
public int index;
/*public String fanId;
public String strAmount;*/
public InfoRowdata(boolean isclicked,int index/*,String fanId,String strAmount*/)
{
this.index=index;
this.isclicked=isclicked;
/*this.fanId=fanId;
this.strAmount=strAmount;*/
}
}
我的MainXml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/llChb"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
这是ListView的自定义RowXml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/rlInner"
android:layout_width="fill_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tvContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Medium Text"
android:textColor="#000000" />
<CheckBox
android:id="@+id/chbContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</RelativeLayout>
</RelativeLayout>
答案 1 :(得分:3)
listView中的视图被回收,这意味着当视图离开屏幕时,它将被重用,以便显示下一个视图。 这也就是说,如果你没有将复选框设置为你想要的任何状态,它将保持状态不受循环视图的影响。
您使用什么类型的适配器?
答案 2 :(得分:0)
使用此
ViewHolder holder;
holder = new ViewHolder();
convertView = mInflator.inflate(R.layout.listviewrow, null);
holder.text = (TextView)convertView.findViewById(R.id.text);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
而不是
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = mInflator.inflate(R.layout.listviewrow, null);
holder.text = (TextView)convertView.findViewById(R.id.text);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
我遇到了同样的问题。也许你会得到答案!
答案 3 :(得分:-1)
如果你仍在寻找答案,那就去了。有些在stackoverflow中被注释,如果我们使用自定义适配器或自定义ListView,那么我们必须记住复选框状态。但没必要记住。 ListView会记住它。因此,在实施之前,请了解以下内容。
一旦你学到了上面提到的要点,那么回答非常简单。 ListView将记住所有状态,并且可以使用isItemChecked通过将position作为参数传递来提取。