2016-11-14 147 views
0

如何檢索單選按鈕值的數據並從android中的sqlite中選擇單選按鈕?

private void collect_data() { 
 

 
     //----- collection & validation for is_required 
 
     String the_choice = ""; 
 
     at_leaset_one_checked = false; 
 
     for (RadioButton rb : allRb) { 
 
      if (rb.isChecked()) { 
 
       at_leaset_one_checked = true; 
 
       the_choice = rb.getText().toString(); 
 
      } 
 
     } 
 

 
     if (the_choice.length() > 0) { 
 
      Answers.getInstance().put_answer(textview_q_title.getText().toString(), the_choice); 
 
      Toast.makeText(getActivity(),"Your ques and asn is:" +textview_q_title.getText() +the_choice, Toast.LENGTH_LONG).show(); 
 
     } 
 

 

 
     
 
     } 
 
     q_data = (Question) getArguments().getSerializable("data"); 
 
     ques_type = (q_data.getQuestionType()); 
 
     ques = textview_q_title.getText().toString(); 
 
     ans = the_choice; 
 
     DbHelper db = new DbHelper(getActivity()); 
 
     if(db.QuesdescExist(ques)) 
 
     { 
 

 
      db.updatedata(ques,ans); 
 
     } 
 
     else { 
 
      db.insertdata(,ques_type ,ques, ans, date, status); 
 
     } 
 

 

 
    } 
 

 

 
    @Override 
 
    public void onActivityCreated(Bundle savedInstanceState) { 
 
     super.onActivityCreated(savedInstanceState); 
 

 

 
     mContext = getActivity(); 
 
     q_data = (Question) getArguments().getSerializable("data"); 
 

 
     textview_q_title.setText(q_data.getQuestionTitle()); 
 

 

 
     List<String> qq_data = q_data.getChoices(); 
 
     if (q_data.getRandomChoices()) { 
 
      Collections.shuffle(qq_data); 
 
     } 
 

 
     for (String choice : qq_data) { 
 
      RadioButton rb = new RadioButton(mContext); 
 
      rb.setText(Html.fromHtml(choice)); 
 
      rb.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); 
 
      rb.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
 
      radioGroup.addView(rb); 
 
      allRb.add(rb); 
 

 
      rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
 
       @Override 
 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
 
        collect_data(); 
 
       } 
 
      }); 
 
     } 
 

 
    

我存儲問題的數據在數據庫中的單選按鈕被點擊的答案,但是當我去到另一個活動我想要回去問這個問題應該有它的回答點擊。請任何人都可以幫助我。

預先感謝您

+0

把一些代碼,你會怎麼做 – Sayem

+0

列表 qq_data = q_data.getChoices();如果(q_data.getRandomChoices()){ Collections.shuffle(qq_data); (String choice:qq_data){ RadioButton rb = new RadioButton(mContext); rb.setText(Html.fromHtml(choice)); rb.setTextSize(TypedValue.COMPLEX_UNIT_SP,16); rb.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)); radioGroup.addView(rb); allRb.add(rb) – radhika

+0

您可以通過編輯選項編輯帖子。請把你的代碼放在那裏。 – Sayem

回答

0

檢查這個

import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.RadioButton; 
    import android.widget.RadioGroup; 
    import android.widget.Toast; 

    public class MyAndroidAppActivity extends Activity { 

     private RadioGroup radioGroup; 
     private RadioButton radioButton; 
     private Button btnDisplay; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     addListenerOnButton(); 

     } 

     public void addListenerOnButton() { 

     radioGroup = (RadioGroup) findViewById(R.id.radio); 
     btnDisplay = (Button) findViewById(R.id.btnDisplay); 

     btnDisplay.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

        // get selected radio button from radioGroup 
       int selectedId = radioGroup.getCheckedRadioButtonId(); 

       // find the radiobutton by returned id 
       radioButton = (RadioButton) findViewById(selectedId); 

       Toast.makeText(MyAndroidAppActivity.this, 
        radioButton.getText(), Toast.LENGTH_SHORT).show(); 

      } 

     }); 

     } 
    } 

XML

<RadioGroup 
     android:id="@+id/radio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radioMale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_male" 
      android:checked="true" /> 

     <RadioButton 
      android:id="@+id/radioFemale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_female" /> 

    </RadioGroup> 
+0

這不是我要求的。抱歉。 – radhika

+0

我已經將值存儲在數據庫中,現在我希望它檢索數據並希望通過根據先前存儲的值以編程方式選擇單選按鈕來在活動中顯示它。 – radhika