有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在自定义listview中从无线组获取数据

我想从自定义listview中的radio group中检索数据,并将数据保存到数据库,但当我想使用以下代码时,应用程序崩溃。我尝试了不同的方法,但都不管用

@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;
    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    试试这个:

    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);