我试图为包含我的复选框的列表创建BaseAdapter。问题是,当我尝试获取每个行项的标记时,它返回null。
适配器:
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class TaskListAdapter extends BaseAdapter {
Context context;
List<Patient> rowItems;
public TaskListAdapter(Context context, List<Patient> items) {
super();
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
CheckBox checkbox;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent, false);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.firstLine);
holder.txtDesc = (TextView) convertView.findViewById(R.id.secondLine);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Patient rowItem = (Patient) getItem(position);
holder.txtDesc.setText(rowItem.getName());
holder.txtTitle.setText(rowItem.getAddress());
holder.checkbox.setChecked(rowItem.isSelected());
holder.checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox);
Patient rowItem = (Patient) checkbox.getTag();
// rowItem is null now for some reason
rowItem.setSelected(checkbox.isChecked());
}
});
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
模型类:
public class Patient {
private String name;
private String address;
private boolean checked;
public Patient(String name, String address, boolean checked) {
super();
this.name = name;
this.address = address;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public boolean isSelected() {
return checked;
}
public void setSelected(boolean checked) {
this.checked = checked;
}
}
我试图获取每一行的标记,以便在滚动视图时保存复选框状态。 感谢。
答案 0 :(得分:1)
您实际上从未为CheckBox视图设置标记(R.id.checkbox)。至少没有您共享的代码中的位置。因此OnClickListener中的rowItem只能为null。修复:
holder.checkbox.setTag(rowItem);
您可以只引用下面显示的rowItem,而不是将Patient设置为CheckBox的标记:
final Patient rowItem = (Patient) getItem(position);
holder.txtDesc.setText(rowItem.getName());
holder.txtTitle.setText(rowItem.getAddress());
holder.checkbox.setChecked(rowItem.isSelected());
holder.checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox);
rowItem.setSelected(checkbox.isChecked());
}
});