2015-05-24 41 views
1

我想從Fragment中的所有視圖中選擇的單選按鈕收集數據並將其發佈到服務器,我如何收集數據?至少有人能幫我敬酒嗎?如何從片段中的不同視圖收集值?

注:此刻我已經在做一個屏幕,但我想所有屏幕的集體數據。

吐司

Button myButton = (Button) layoutView.findViewById(R.id.findSelected); 
      myButton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        StringBuffer responseText = new StringBuffer(); 
        responseText.append(""); 

        // Get selected radiobuttons 
        if (radioGroup1.getCheckedRadioButtonId() != -1) { 
         text1 = btn.getText().toString(); 
         Log.d("Button", "Text 1 : " + text1); 
        } 

        if (radioGroup2.getCheckedRadioButtonId() != -1) { 
         text2 = btn2.getText().toString(); 
         Log.d("Button", "Text 2 : " + text2); 
        } 


        Toast.makeText(
          thiscontext, 
          "Data Posting : APPLICATION : " 
            + text1 + " \nDEVICE : " + text2, 
          Toast.LENGTH_LONG).show(); 


       } 
      }); 

全碼:

LayoutFragment.java

public class LayoutFragment extends Fragment { 
    int fragVal; 
    private String[] application = { "Country1", "Country2", "Country3", "Country4", "Country5", "Country6", "Country7", "Country8" };      
    private String[] device = { "Country9", "Country10", "Country11", "Country12", "Country13", "Country14", "Country15", "Country16" }; 
    private RadioGroup radioGroup1; 
    private RadioGroup radioGroup2; 
    private RadioButton btn; 
    private RadioButton btn2; 
    private String text1; 
    private String text2; 
    RadioButton button1; 
    RadioButton button2; 
    Button selectall; 
    Context thiscontext; 

    static LayoutFragment init(int val) { 
     LayoutFragment truitonFrag = new LayoutFragment(); 
     // Supply val input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("val", val); 
     truitonFrag.setArguments(args); 
     return truitonFrag; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     fragVal = getArguments() != null ? getArguments().getInt("val") : 1; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     thiscontext = container.getContext(); 
     View layoutView = inflater.inflate(R.layout.activity_main, container, false); 

     Button myButton = (Button) layoutView.findViewById(R.id.findSelected); 
     myButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       StringBuffer responseText = new StringBuffer(); 
       responseText.append(""); 

       // Get selected radiobuttons 
       if (radioGroup1.getCheckedRadioButtonId() != -1) { 
        text1 = btn.getText().toString(); 
        Log.d("Button", "Text 1 : " + text1); 
       } 

       if (radioGroup2.getCheckedRadioButtonId() != -1) { 
        text2 = btn2.getText().toString(); 
        Log.d("Button", "Text 2 : " + text2); 
       } 


       Toast.makeText(
         thiscontext, 
         "Data Posting : APPLICATION : " 
           + text1 + " \nDEVICE : " + text2, 
         Toast.LENGTH_LONG).show(); 


      } 
     }); 


     //Draw Radiobuttons 

     radioGroup1 = (RadioGroup) layoutView.findViewById(R.id.radio1); 
     radioGroup2 = (RadioGroup) layoutView.findViewById(R.id.radio2); 

     ViewGroup hourButtonLayout = (ViewGroup) layoutView.findViewById(R.id.radio1); 
     for (int i = 0; i < application.length; i++) { 
      button1 = new RadioButton(thiscontext); 
      button1.setId(i); 
      button1.setText(application[i]); 
      hourButtonLayout.addView(button1); 

      radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
         public void onCheckedChanged(RadioGroup mRadioGroup2, 
           int checkedId2) { 
          for (int i = 0; i < mRadioGroup2.getChildCount(); i++) { 
           btn = (RadioButton) mRadioGroup2.getChildAt(i); 
           int t = mRadioGroup2.getId(); 
           System.out.println(t); 

           if (btn.getId() == checkedId2) { 
            text1 = btn.getText().toString(); 
            Toast.makeText(thiscontext, 
              "You selected : " + text1, 
              Toast.LENGTH_SHORT).show(); 
            return; 
           } 
          } 
         } 
        }); 

     } 

     ViewGroup hourButtonLayout2 = (ViewGroup) layoutView.findViewById(R.id.radio2); 
     for (int i = 0; i < device.length; i++) { 
      button2 = new RadioButton(thiscontext); 
      button2.setId(i); 
      button2.setText(device[i]); 
      hourButtonLayout2.addView(button2); 

      radioGroup2 
        .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
         public void onCheckedChanged(RadioGroup mRadioGroup, 
           int checkedId) { 
          for (int i = 0; i < mRadioGroup.getChildCount(); i++) { 
           btn2 = (RadioButton) mRadioGroup.getChildAt(i); 
           int t = mRadioGroup.getId(); 
           System.out.println(t); 

           if (btn2.getId() == checkedId) { 
            text2 = btn2.getText().toString(); 
            Toast.makeText(thiscontext, 
              "You selected : " + text2, 
              Toast.LENGTH_SHORT).show(); 
            return; 
           } 
          } 
         } 
        }); 

     } 



     return layoutView; 
    } 
} 

回答

0

添加一個公共方法在每一個片段,像

public HashMap<String, Boolean> getRadiosStatus{ 
    //TODO put every radio button's status to hashmap and return it. 
} 

然後,調用此方法在活動。您可以通過調用所有相同的方法從所有片段中獲取所有狀態。

+0

只有一個片斷顯示動態視圖,不確定您的建議是否理想 – user1223035

+0

根據這些視圖創建布爾陣列如何?無線電狀態改變時,根據ID更改相應的元素。 – Swordsman

相關問題