2013-11-03 182 views
1

我在我的應用程序中有一個大問題,我已經來到這裏,我學會了如何創建一個數據庫,以及如何插入單選按鈕和編輯文本值分開,現在我什麼我試圖做的是插入兩個單選按鈕的選定值,並單擊按鈕(ADD)編輯文本,但我面臨一個問題,值編輯文本不考慮我會很感激,如果有人幫助我! 這裏是代碼:插入值單選按鈕和編輯文本點擊按鈕

 public long insertMENAGE(String Region, String Provence_prefecture , String Commune_Arrondissement ,String N_district, String N_M_district, int echantillon_principal, int Statut_enquêté ) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(col_Region,Region); 
     initialValues.put(col_Provence_prefecture ,Provence_prefecture); 
     initialValues.put(col_Commune_Arrondissement,Commune_Arrondissement); 
     initialValues.put(col_N_district,N_district); 
     initialValues.put(col_N_M_district,N_M_district); 
     initialValues.put(col_echantillon_principal,echantillon_principal); 
     initialValues.put(col_Statut_enquêté,Statut_enquêté); 

    return db.insert(MENAGE,null, initialValues); 
    } 


public class ActivityUn extends Activity { 
DBAdapter db = new DBAdapter(this); 
    public void ajouter(View v) { 
    db.open(); 
    db.insertMENAGE(rm_1ts,rm_2ts,rm_3ts,rm_4ts,rm_5ts,a,b); 
    db.close(); 

    } 

private RadioGroup rm_13; 
private RadioButton rm_13_1 ; 
private RadioButton rm_13_2 ; 

private RadioGroup rm_14; 
private RadioButton rm_14_1 ; 
private RadioButton rm_14_2 ; 
private RadioButton rm_14_3; 
private RadioButton rm_14_4 ; 

int a; 
int b; 


EditText rm_1; 
EditText rm_2; 
EditText rm_3 ;  
EditText rm_4 ; 
EditText rm_5; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 


    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_un); 

    String rm_1ts = rm_1.getText().toString(); 
    String rm_2ts = rm_2.getText().toString(); 
    String rm_3ts = rm_3.getText().toString(); 
    String rm_4ts = rm_4.getText().toString(); 
    String rm_5ts = rm_5.getText().toString(); 

    Button suivant = (Button)findViewById(R.id.activityDeux); 
    suivant.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      Intent l = new Intent(ActivityUn.this,ActivityDeux.class); 
      startActivity(l); 
      } 
     }); 

} 

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_13_1: 
        if (checked) 
          a=0; 
         break; 
       case R.id.rm_13_2: 
        if (checked) 
         a=1; 
         break; 
       case R.id.rm_14_1: 
        if (checked) 
         b=0; 
        break; 
       case R.id.rm_14_2: 
        if (checked) 
         b=1; 
        break; 
       case R.id.rm_14_3: 
        if (checked) 
         b=2; 
        break; 
       case R.id.rm_14_4: 
        if (checked) 
         b=3; 
        break; 
      } 

     Button bton = (Button)findViewById(R.id.ajoutUn); 
     bton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       ajouter(v); 
       Toast.makeText(getApplicationContext(), "Données Enregistrées", Toast.LENGTH_SHORT).show(); 
      } 


     }); 


} 

}

回答

0

所有類ActivityUn代碼

public class ActivityUn extends Activity { 

//DB Class 
DBAdapter db = new DBAdapter(this); 

//Execute Save Values 
public void ajouter(View v) { 
    db.open(); 
    db.insertMENAGE(rm_1ts,rm_2ts,rm_3ts,rm_4ts,rm_5ts,a,b); 
    db.close(); 
} 

//Register Radio Group 
RadioGroup rm_13,rm_14; 

//Register Radio Button's 
RadioButton rm_13_1,rm_13_2,rm_14_1,rm_14_2,rm_14_3,rm_14_4 ; 


    int a = 0, 
     b = 0; 

//Register Edit Text 
EditText rm_1,rm_2,rm_3,rm_4,rm_5; 

//Now Register Empty Values  
    String rm_1ts = "", 
     rm_2ts = "", 
     rm_3ts = "", 
     rm_4ts = "", 
     rm_5ts = ""; 


/**************************************** 
*Activity Created we need to Register All layouts or we will get Errors 
*/   
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_un); 


//Register Edit Texts 
rm_1 = (EditText) findViewById(R.id.rm_1); 
rm_2 = (EditText) findViewById(R.id.rm_2); 
rm_3 = (EditText) findViewById(R.id.rm_3);  
rm_4 = (EditText) findViewById(R.id.rm_4); 
rm_5 = (EditText) findViewById(R.id.rm_5); 

//Register Radios 
rm_13_1 = (RadioButton) findViewById(R.id.rm_13_1); 
rm_13_2 = (RadioButton) findViewById(R.id.rm_13_2); 
rm_14_1 = (RadioButton) findViewById(R.id.rm_14_1); 
rm_14_2 = (RadioButton) findViewById(R.id.rm_14_2); 
rm_14_3 = (RadioButton) findViewById(R.id.rm_14_3); 
rm_14_4 = (RadioButton) findViewById(R.id.rm_14_4); 

//Register Radio Groups, you dont have to do that if you don't using them 
rm_13 = (RadioGroup) findViewById(R.id.rm_13); 
rm_14 = (RadioGroup) findViewById(R.id.rm_14); 

    //Button to start new Activity 
    Button suivant = (Button)findViewById(R.id.activityDeux); 
      suivant.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

      Intent l = new Intent(ActivityUn.this,ActivityDeux.class); 
       startActivity(l); 

      }}); 

//Button to Save information 
((Button)findViewById(R.id.ajoutUn)).setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 

//Get EditText Values and Add them to the Strings started as blank Strings above 
    rm_1ts = rm_1.getText().toString(); 
    rm_2ts = rm_2.getText().toString(); 
    rm_3ts = rm_3.getText().toString(); 
    rm_4ts = rm_4.getText().toString(); 
    rm_5ts = rm_5.getText().toString(); 

//Execute save info 
ajouter(v); 

Toast.makeText(getApplicationContext(), "Données Enregistrées", Toast.LENGTH_SHORT).show(); 

}}); 

} 


     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_13_1: 
          if (checked) 
            a=0; 
           break; 
         case R.id.rm_13_2: 
          if (checked) 
           a=1; 
           break; 
         case R.id.rm_14_1: 
          if (checked) 
           b=0; 
          break; 
         case R.id.rm_14_2: 
          if (checked) 
           b=1; 
          break; 
         case R.id.rm_14_3: 
          if (checked) 
           b=2; 
          break; 
         case R.id.rm_14_4: 
          if (checked) 
           b=3; 
          break; 
        } 

     } 
+0

感謝您迴應:)我明白,替換,但我已經使用這個解決方案,但它不工作!我嘗試了所有可能的組合,但是錯誤仍然存​​在,所以如果我按照你的建議去做,db.insertMENAGE方法將會出錯,它不會識別這些變量。 –

+0

你能用'db.insertMENAGE'函數更新你的問題嗎? –

+0

這是它,我希望你有sugestions。 –