我有自定义列表视图,其中包含复选框和Textview。由于某些原因,我无法使用ListActivity。好吧,它很容易使用listview,但我不知道为什么Check框的行为不同。
首先让我告诉你我在做什么,下面是我的适配器的一部分
private static class ViewHolder { //My View Hold
TextView tvParentGradeName;
CheckBox cbParentClassesOfGrades;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
final ClassModelInfo user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
final ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(resourceLayout, parent, false);
viewHolder.tvParentGradeName = (TextView) convertView.findViewById(R.id.tvParentGradeName);
viewHolder.cbParentClassesOfGrades = (CheckBox) convertView.findViewById(R.id.cbParentClassesOfGrades);
convertView.setTag(viewHolder);
viewHolder.cbParentClassesOfGrades.setChecked(false);
//Following list consist of ids of items whoes check box are checked alredy
if(selectedItemsList!= null && selectedItemsList.size()>0){
for(int i = 0 ; i<selectedItemsList.size();i++){
if(user.getClassID().matches(""+selectedItemsList.get(i))){
viewHolder.cbParentClassesOfGrades.setChecked(true);
}
}
}
viewHolder.cbParentClassesOfGrades.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
listClass.get(getPosition).setSetSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
// if(callback!=null)
// callback.checkBoxChecked();
if (isChecked) {
selectedItemsList.add(Integer.parseInt(user.getClassID()));
} else {
// selectedItemsList.remove(classsLevelClickedItems.getId());
Iterator<Integer> myItr = selectedItemsList.iterator();
while (myItr.hasNext()) {
Integer id = myItr.next();
System.out.println("Next name is: " + id);
if (id == Integer.parseInt(user.getClassID())) {
myItr.remove();
}
}
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.tvParentGradeName, viewHolder.tvParentGradeName);
convertView.setTag(R.id.cbParentClassesOfGrades, viewHolder.cbParentClassesOfGrades);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.cbParentClassesOfGrades.setTag(position); // This line is important.
viewHolder.tvParentGradeName.setText(user.getClassName()+" ("+gradeDictionary.get(Integer.parseInt(user.getGradeID()))+")");
viewHolder.cbParentClassesOfGrades.setChecked(user.isSetSelected());
return convertView;
}
**现在,如果我删除以下行,则复选框行为会变得奇怪,例如,如果我选中第4个复选框,并开始滚动列表视图,我会看到已经检查过的复选框,我不知道这种行为是什么**
viewHolder.cbParentClassesOfGrades.setTag(位置); //这条线很重要 viewHolder.tvParentGradeName.setText(user.getClassName()+“&gt;(”+ gradeDictionary.get(Integer.parseInt(user.getGradeID()))+“)”); viewHolder.cbParentClassesOfGrades.setChecked(user.isSetSelected());
所以基本上正如我之前提到的,我上面提到的最大问题是一些问题
1)如何使用复选框创建一致的列表视图,我的意思是listview应该记住单击了哪个复选框
2)如何在listview的整行上创建点击监听器,这将帮助用户点击该项并标记复选框(选中并取消选中),这样用户就无需专门勾选复选框。
请告诉我如何改善这一点?这是我的日子。
注意:我已经搜索了这个问题很多,在我的代码中,带注释的行是我从网上复制的行。
答案 0 :(得分:0)
您需要跟踪已检查和未检查的位置,并将其标记为您的采用者。在列表视图中,它是期望的行为。你必须在代码中处理它。 为列表项创建一个数组,当单击一个复选框时,更新该位置的数组,并在适配器标记中选中/取消选中数组的相应值。
public class MyCustomAdapter extends ArrayAdapter<ApplicationInfo> {
private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck;
public MyCustomAdapter(Context context, List<ApplicationInfo> myList) {
super(context, NO_SELECTION);
appInfoList = myList;
ctx=context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm = context.getPackageManager();
positionArray = new ArrayList<Boolean>(myList.size());
for(int i =0;i<myList.size();i++){
positionArray.add(false);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return appInfoList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
if(row==null){
row = mInflater.inflate(R.layout.testlayout, null);
// visiblePosArray[position%visiblePosArray.length]=position;
holder = new Holder();
holder.appIcon = (ImageView)row.findViewById(R.id.imageView1);
holder.ckbox =(CheckBox)row.findViewById(R.id.checkBox1);
row.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
holder.ckbox.setOnCheckedChangeListener(null);
}
holder.ckbox.setFocusable(false);
holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
holder.ckbox.setChecked(positionArray.get(position));
holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked ){
System.out.println(position+"--- :)");
positionArray.set(position, true);
}else
positionArray.set(position, false);
}
});
return row;
}
static class Holder
{
ImageView appIcon;
CheckBox ckbox;
}
}