我在ListItems中选择CheckBoxes时遇到问题。每个ListItem都有一个CheckBox,当单击ListItem时,应激活CheckBox。问题是CheckBox随机激活。例如:当我有2个ListItem并单击ListItem1时,它的CheckBox被激活。当我再次点击时,ListItem2的CheckBox也被激活。当我再次单击时,ListItem1的CheckBox被取消激活。当我再次点击时,ListItem2的CheckBox被停用。当我再次点击它时,它从头开始。我理解ListView重用了Items,我必须使用getView()方法,但我无法使它工作。我必须提到我是一个血腥的初学者,所以请原谅我,如果一些代码根本没有意义。
答案 0 :(得分:0)
尝试使用 setOnItemSelectedListener 而不是 setOnItemClickListener 。我相信点击事件会更频繁地被调用。
答案 1 :(得分:0)
您需要将项目状态存储在您的适配器中,不是在View本身中,因为它可能是一个重用的,就像您说的那样。您拨打super.getView()
的电话会为您填写正确的文字,但不会更改复选框状态。保持一个bool数组反映每个复选框的状态,并在getView()中应用状态,然后返回视图:
CheckBox cb = (CheckBox) view.findViewById(R.id.cb);
cb.setChecked(cbStates[position]);
答案 2 :(得分:0)
您需要创建自定义阵列适配器和所有设置。
这是我为ListView for ChekBoxList创建的Activity类
<强> ActivityGamesList.java 强>
package com.rdc.gameListApp;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;
public class ActivityGamesList extends Activity {
private ListView listGames = null;
private DBAdapter database = null;
private Context context = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_games);
final CheckBox favBox = (CheckBox) findViewById(R.id.favbuttonInList);
listGames = (ListView) findViewById(R.id.listViewGame);
context = getApplicationContext();
database = new DBAdapter(context);
//here I am getting the data from database and putting in to List
List<String> gamelistArrayR = database.selectAllName();
// custom adapter for adding fav box
//make sure here "gamelistArrayR" is List and type is String
listGames.setAdapter(new CustomAdapter(this, gamelistArrayR));
//listener for list view
listGames.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "You have selected: " +
position+" no row", Toast.LENGTH_SHORT).show();
}
});
}
}
及以下是自定义阵列适配器
<强> CustomAdapter.java 强>
package com.rdc.gameListApp;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
public CustomAdapter(Context context, List<String> gamelistArrayR) {
super(context, R.layout.list_row_custom, gamelistArrayR);
this.context = context;
this.values = gamelistArrayR;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_row_custom, parent, false);
//Game Name Text in list
TextView textView = (TextView) rowView.findViewById(R.id.txtGameNameInList);
// Check Box in list
CheckBox favBox = (CheckBox) rowView.findViewById(R.id.favbuttonInList);
textView.setText(values.get(position));
//listener for check box
favBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Boolean flag = isChecked;
if(flag){
Log.v("Debug", "step no 1");
Toast.makeText(context, "Marked as Favorite Game!", 1).show();
}
}
});
return rowView;
}
}
我得到了这样的输出..
修改强>
当您滚动列表时,chekboxrs将被取消选中,因为或重新创建视图。
所以你需要存储复选框的状态,你应该检查下面的链接