如何在android中将radioButton分配给ListView

时间:2018-01-04 11:06:53

标签: radio-button

在我的应用程序中,我有一个ListView来包含5个测验问题以及他们在服务器中的json文件中的答案。我希望选择是Radio Buttons但是当我的应用程序在模拟器上运行时,我可以看到问题和答案,但是在点击其中一个单选按钮之后,还有一些其他问题'答案检查单选按钮。我怎么能防止这种情况?

这是我的适配器类:

public class FormSoruAdapter extends ArrayAdapter<FormSoru>{

    List<FormSoru> formSoruList;
    Context context;
    private LayoutInflater mInflater;

    //Constructor method
    public FormSoruAdapter(Context context, List<FormSoru> objects){
        super(context, 0, objects);
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        formSoruList = objects;
    }

    @Override
    public FormSoru getItem(int position){
        return formSoruList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder vh;
        if (convertView == null){
            View view = mInflater.inflate(R.layout.list_soru,parent,false);
            vh = ViewHolder.create((LinearLayout) view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        FormSoru item = getItem(position);
        vh.textViewSoruNo.setText(item.getId());
        vh.textViewSoru.setText(item.getSoru());
        String cevap1 = item.getCevap1();
        String cevap2 = item.getCevap2();
        String cevap3 = item.getCevap3();
        String cevap4 = item.getCevap4();
        String cevap5 = item.getCevap5();

        if (cevap1 == "" && cevap2 == "" && cevap3 == "" && cevap4 == "" && cevap5 == ""){
            vh.radioButtonCevap1.setVisibility(View.GONE);
            vh.radioButtonCevap2.setVisibility(View.GONE);
            vh.radioButtonCevap3.setVisibility(View.GONE);
            vh.radioButtonCevap4.setVisibility(View.GONE);
            vh.radioButtonCevap5.setVisibility(View.GONE);
        } else {
            vh.radioButtonCevap1.setText(item.getCevap1());
            vh.radioButtonCevap2.setText(item.getCevap2());
            vh.radioButtonCevap3.setText(item.getCevap3());
            vh.radioButtonCevap4.setText(item.getCevap4());
            vh.radioButtonCevap5.setText(item.getCevap5());
        }

        return vh.rootView;

    }

    private static class ViewHolder {
        public final LinearLayout rootView;
        public final TextView textViewSoruNo;
        public final TextView textViewSoru;
        public final RadioButton radioButtonCevap1;
        public final RadioButton radioButtonCevap2;
        public final RadioButton radioButtonCevap3;
        public final RadioButton radioButtonCevap4;
        public final RadioButton radioButtonCevap5;

        private ViewHolder(LinearLayout rootView, TextView textViewSoruNo, TextView textViewSoru, RadioButton radioButtonCevap1, RadioButton radioButtonCevap2, RadioButton radioButtonCevap3, RadioButton radioButtonCevap4, RadioButton radioButtonCevap5){
            this.rootView = rootView;
            this.textViewSoruNo = textViewSoruNo;
            this.textViewSoru = textViewSoru;
            this.radioButtonCevap1 = radioButtonCevap1;
            this.radioButtonCevap2 = radioButtonCevap2;
            this.radioButtonCevap3 = radioButtonCevap3;
            this.radioButtonCevap4 = radioButtonCevap4;
            this.radioButtonCevap5 = radioButtonCevap5;
        }

        public static ViewHolder create(LinearLayout rootView){
            TextView textViewSoruNo = (TextView) rootView.findViewById(R.id.textViewSoruNo);
            TextView textViewSoru = (TextView) rootView.findViewById(R.id.textViewSoru);
            RadioButton radioButtonCevap1 = (RadioButton) rootView.findViewById(R.id.radioCevap1);
            RadioButton radioButtonCevap2 = (RadioButton) rootView.findViewById(R.id.radioCevap2);
            RadioButton radioButtonCevap3 = (RadioButton) rootView.findViewById(R.id.radioCevap3);
            RadioButton radioButtonCevap4 = (RadioButton) rootView.findViewById(R.id.radioCevap4);
            RadioButton radioButtonCevap5 = (RadioButton) rootView.findViewById(R.id.radioCevap5);

            return new ViewHolder(rootView, textViewSoruNo, textViewSoru, radioButtonCevap1, radioButtonCevap2,
                    radioButtonCevap3, radioButtonCevap4,radioButtonCevap5);
        }
    }
}

我的片段是:

public class FormFragment extends Fragment {

private ListView listView;
private View parentView;

private ArrayList<FormSoru> formSoruList;
private FormSoruAdapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_anket_soru, container, false);
}

@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    formSoruList = new ArrayList<>();

    parentView = view.findViewById(R.id.parentLayout);
    listView = (ListView) view.findViewById(R.id.listViewSorular);



    APIService apıService = RetroClient.getApiService();

    Call<FormSorular> call = apıService.getFormSorular();

    call.enqueue(new Callback<FormSorular>() {
        @Override
        public void onResponse(Call<FormSorular> call, Response<FormSorular> response) {
            if (response.isSuccessful()){
                formSoruList = response.body().getFormSorular();

                adapter = new FormSoruAdapter(getActivity(),formSoruList);
                listView.setAdapter(adapter);
            }
        }

        @Override
        public void onFailure(Call<FormSorular> call, Throwable t) {

        }
    });


}

}

我的片段布局是:

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dear User"
        android:layout_margin="10dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:layout_margin="8dp"/>

    <ListView
        android:id="@+id/listViewSorular"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Görüşleriniz"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Gönder"/>

</LinearLayout>

我的list_soru布局包含问题:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textViewSoruNo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Soru No"
    android:layout_margin="10dp"/>

<TextView
    android:id="@+id/textViewSoru"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Soru"
    android:layout_margin="8dp"/>

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

    <RadioButton
        android:id="@+id/radioCevap1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cevap1"/>

    <RadioButton
        android:id="@+id/radioCevap2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cevap2"/>

    <RadioButton
        android:id="@+id/radioCevap3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cevap3"/>

    <RadioButton
        android:id="@+id/radioCevap4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cevap4"/>

    <RadioButton
        android:id="@+id/radioCevap5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cevap5"/>

</RadioGroup>

1 个答案:

答案 0 :(得分:0)

当用户选择其中一个单选按钮时,相应的RadioButton对象会收到一个点击事件。

要为按钮定义click事件处理程序,请将android:onClick属性添加到XML布局中的<RadioButton>元素。此属性的值必须是要响应click事件而调用的方法的名称。然后,托管布局的Activity必须实现相应的方法。

例如,这里有几个RadioButton对象:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
   <RadioButton android:id="@+id/radio_pirates"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/pirates"
       android:onClick="onRadioButtonClicked"/>
   <RadioButton android:id="@+id/radio_ninjas"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/ninjas"
       android:onClick="onRadioButtonClicked"/>
</RadioGroup>

在承载此布局的Activity中,以下方法处理两个单选按钮的单击事件:

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.radio_pirates:
          if (checked)
            // Pirates are the best
            break;
        case R.id.radio_ninjas:
          if (checked)
           // Ninjas rule
            break;
      } 
    }