我有一个DisplayStudentName.java文件,这是我的主要活动文件。有一个模型类用于获取和设置数据,还有一个MyCustomAdapter.java文件,它扩展了ArrayAdapter以实现listview功能。
单击该复选框,但未在arraylist中添加值(命名数据;键入String)。
DisplayStudentName.java
public class MyCustomAdapter extends ArrayAdapter {
public ArrayList<Student> personList=null;
private Context context=null;
private static LayoutInflater inflater=null;
private ArrayList<String> data=null;
private View vi;
ViewHolder holder=null;
public MyCustomAdapter(Context context, int resource, ArrayList<Student> personList) {
super(context, resource, personList);
this.context=context;
this.personList=personList;
}
private class ViewHolder{
TextView roll;
TextView name;
CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent){
Log.v("ConvertView", String.valueOf(position));
if(convertView==null){
LayoutInflater vi = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_item, null);
holder=new ViewHolder();
holder.roll=(TextView)convertView.findViewById(R.id.roll);
holder.name=(TextView)convertView.findViewById(R.id.name);
holder.check=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
Student student=personList.get(position);
holder.roll.setText(student.getRollno());
holder.name.setText(student.getName());
holder.check.setTag(student);
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
data.add(holder.check.getText().toString());
} else {
data.remove(holder.check.getText().toString());
}
}
});
return convertView;
}
MyCustomAdapter.java
public class Student {
String rollno;
String name;
Boolean checkbox;
public Student(){
}
public Student(String rollno, String name, Boolean status){
super();
this.rollno=rollno;
this.name=name;
this.checkbox=status;
}
public String getRollno(){
return rollno;
}
public void setRollNo(String rollno){
this.rollno=rollno;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Boolean isCheckbox(){
return checkbox;
}
public void setCheckbox(boolean checkbox) {
this.checkbox= checkbox;
}
}
Student.java -Model class
MyGreatProtocol
请告诉我我的代码有什么问题? }
答案 0 :(得分:1)
每当您对数据集进行任何更改时,请致电notifyDataSetChanged()
。
因此会是这样的:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
data.add(holder.check.getText().toString());
} else {
data.remove(holder.check.getText().toString());
}
notifyDataSetChanged();
}