我正在尝试获取复选框的选定值并将其存储在数据库中。我可以单击 ListView 中的复选框。但是由于这行代码“subject.setSelected(cb.isChecked());”帮助,我得到了 NullPointer 异常!
顺便说一句,我正在学习本教程! http://sunil-android.blogspot.com/2013/04/android-listview-checkbox-example.html?showComment=1397151611795#c1803690674156959933
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserRecordHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserRecordHolder();
holder.txtName=(TextView) row.findViewById(R.id.ttvSubjectNamePSS);
holder.txtCode=(TextView) row.findViewById(R.id.ttvSubjectCodePSS);
holder.cbSubj=(CheckBox) row.findViewById(R.id.cbPSS);
row.setTag(holder);
holder.cbSubj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
CheckBox cb = (CheckBox) v ;
SbjData subjects = (SbjData) cb.getTag();
Toast.makeText(context, "clicked on : "+ cb.getText() +"is "+cb.isChecked(), 2).show();
//This line shows exception
subjects.setSelected(cb.isChecked());
}
catch(Exception e)
{
Toast.makeText(context, e.toString(), 5).show();
}
}
});
}
else
{
holder = (UserRecordHolder)row.getTag();
}
public class SbjData {
public String sbj_name,sbj_code,sbj_depId,sbj_crcId;
public int sbj_id;
boolean selected = false;
public SbjData() {
super();
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public boolean isSelected() {
return selected;
}
}