我想从自定义列表视图中的无线电组中检索数据并将该数据保存到数据库但是当我想使用以下代码时,应用程序崩溃了。我尝试了不同的方法,但都没有。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_attendence);
ListView lv = (ListView) findViewById(R.id.lvshowattend);
final SQLiteDatabase db = openOrCreateDatabase("my data base", MODE_WORLD_WRITEABLE, null);
Cursor c = db.rawQuery("select * from attendence ", null);
name = new ArrayList<>();
atten = new ArrayList<>();
date = new ArrayList<>();
try {
while (c.moveToNext()) {
name.add(c.getString(0));
atten.add(c.getString(1));
date.add(c.getString(2));
CustomAdapter c1 = new CustomAdapter(ShowAttendence.this, R.layout.row2, R.id.cstname, name);
lv.setAdapter(c1);
}
}
finally {
c.close();
}
}
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.row2, parent, false);
TextView cname = (TextView) v.findViewById(R.id.cstname);
TextView cattend = (TextView) v.findViewById(R.id.cstattend);
RadioGroup crg = (RadioGroup) findViewById(R.id.cstrg);
RadioButton crbpr= (RadioButton) findViewById(R.id.crbpresent);
RadioButton crbab= (RadioButton) findViewById(R.id.crbabsent);
RadioButton crble= (RadioButton) findViewById(R.id.crbleave);
cname.setText(name.get(position));
cattend.setText(atten.get(position));
int selectedId = crg.getCheckedRadioButtonId();
RadioButton rb= (RadioButton) findViewById(selectedId);
return v;
}
}
}
答案 0 :(得分:0)
试试这个:
crg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
//find radiobutton position
int position = group.indexOfChild(radioButton);
RadioButton btn = (RadioButton) crg.getChildAt(position);
String selection = (String) btn.getText();
}
});
而不是
int selectedId = crg.getCheckedRadioButtonId();
RadioButton rb= (RadioButton) findViewById(selectedId);
同时改变这一点;
RadioGroup crg = (RadioGroup) findViewById(R.id.cstrg);
RadioButton crbpr= (RadioButton) findViewById(R.id.crbpresent);
RadioButton crbab= (RadioButton) findViewById(R.id.crbabsent);
RadioButton crble= (RadioButton) findViewById(R.id.crbleave);
到
RadioGroup crg = (RadioGroup) v.findViewById(R.id.cstrg);
RadioButton crbpr= (RadioButton) v.findViewById(R.id.crbpresent);
RadioButton crbab= (RadioButton) v.findViewById(R.id.crbabsent);
RadioButton crble= (RadioButton) v.findViewById(R.id.crbleave);