只需单击一下即可插入多个值单选按钮

时间:2013-10-31 12:21:51

标签: android android-sqlite

我在开发一个由问题和多个单选按钮组成的应用程序的最后一部分,我知道如何插入单选按钮的每个值,但现在我试图插入所选的单选按钮的所有值单击按钮插入,但我有这个错误:java.lang.ILLegalStateException : could not find a method ajouter (View) in activity class

请你知道吗?

public class MainActivity extends Activity {

DBAdapter db = new DBAdapter(this); 
  int x=0;
  int y=0;
  int z=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        db.open();

    //  RadioGroup  radio =(RadioGroup)findViewById(rm_25_1);
        RadioButton rad0 = (RadioButton) findViewById (R.id.rm_25_1_1);
        RadioButton rad1 = (RadioButton) findViewById (R.id.rm_25_1_2);

  //      RadioGroup  Télévision  =(RadioGroup)findViewById(rm_25_2);
        RadioButton tel0 = (RadioButton) findViewById (R.id.rm_25_2_1);
        RadioButton tel1 = (RadioButton) findViewById (R.id.rm_25_2_2);

//          RadioGroup  Réfrigérateur =(RadioGroup)findViewById(rm_25_3);
        RadioButton ref0 = (RadioButton) findViewById (R.id.rm_25_3_1);
        RadioButton ref1 = (RadioButton) findViewById (R.id.rm_25_3_2);

}

public void onRadioButtonClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.rm_25_1_1:
                    if (checked)
                          x=0;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_1_2:
                    if (checked)
                        x=1;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_2_1:
                    if (checked)
                        y=0;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_2_2:
                    if (checked)
                        y=1;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_3_1:
                    if (checked)
                        z=0;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();
                        break;
                case R.id.rm_25_3_2:
                    if (checked)
                        z=1;
                          Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();

            }
       Button bton = (Button)findViewById(R.id.ajout_un);
       bton.setOnClickListener(new View.OnClickListener() { 


          @Override
          public void onClick(View v) {

                ajouter(v);
                Toast.makeText(getApplicationContext(), "Données enregistrées!!! =)", Toast.LENGTH_LONG).show();

          }

          public void  ajouter(View v) {
               db.insertcc(x,y,z);

          }
        });

  }}

这是XML文件:

   <TextView
    android:id="@+id/qm_25_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text=".1.Radio :  "
    />
   <RadioGroup
    android:id="@+id/rm_25_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_25_1_1"
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:text="OUI" />

    <RadioButton
        android:id="@+id/ rm_25_1_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:text="NON" />

</RadioGroup>

   <TextView
    android:id="@+id/qm_25_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text=".2. Télévision :"
    />
   <RadioGroup
    android:id="@+id/rm_25_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_25_2_1"
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:text="Oui" />

    <RadioButton
        android:id="@+id/ rm_25_2_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:text="Non" />

</RadioGroup>

    <TextView
    android:id="@+id/qm_25_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    style = "@style/question"
    android:text=".3. Réfrigérateur :"
    />
   <RadioGroup
       android:id="@+id/rm_25_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:layout_marginTop="12dp"
    android:background ="#FFF8DC"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rm_25_3_1"
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:text="Oui" />

    <RadioButton
        android:id="@+id/rm_25_3_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:text="Non" />
</RadioGroup>
<Button
    android:id="@+id/ajout_un"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="ajouter"
    android:layout_marginTop="26dp"
    android:layout_marginLeft="150dp"
    android:text="ENREGISTRER"
    android:textStyle="bold" />

2 个答案:

答案 0 :(得分:0)

尝试将ajouter(v);来电更改为this.ajouter(v);this应该引用您的OnClickListener类。

答案 1 :(得分:0)

将您的ajouter()方法从click侦听器类中取出,并将其作为您活动的私有成员。