从列表视图中选择单选按钮

时间:2018-04-09 15:38:54

标签: android listview radio-button

我有一个listview;我使用自定义适配器修改了listview,而我的自定义适配器包含一个包含四个单选按钮的无线电组。

因此,对于列表中的每个项目,有四个单选按钮。我想根据radiobutton选择显示不同的Toast消息。我怎么能这样做?

Problem figure

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

Demo10.class ------------

public class Demo10 extends AppCompatActivity implements Listener {

private ListView lv;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo11);

    lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(new ItemAdapter(Demo10.this , this));

}


@Override
public void RadioButton1(String text) {
   Toast.makeText(getApplicationContext() , text , Toast.LENGTH_LONG).show();
}

@Override
public void RadioButton2(String text) {
    Toast.makeText(getApplicationContext() , text , Toast.LENGTH_LONG).show();
}

@Override
public void RadioButton3(String text) {
    Toast.makeText(getApplicationContext() , text , Toast.LENGTH_LONG).show();
}

@Override
public void RadioButton4(String text) {
    Toast.makeText(getApplicationContext() , text , Toast.LENGTH_LONG).show();
}
}

ItemAdapter.class ---------

public class ItemAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;

private String[] q = {
        "Q1",
        "Q2",
        "Q3",
        "Q4"
};

private String[] a1 = {
        "a1(1)",
        "a1(2)",
        "a1(3)",
        "a1(4)"
};

private String[] a2 = {
        "a2(1)",
        "a2(2)",
        "a2(3)",
        "a2(4)"
};

private String[] a3 = {
        "a3(1)",
        "a3(2)",
        "a3(3)",
        "a3(4)"
};

private String[] a4 = {
        "a4(1)",
        "a4(2)",
        "a4(3)",
        "a4(4)"
};

public ItemAdapter(Context c , Listener l) {
    mContext = c;
    callback = l;
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return q.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    RadioGroup rg;
    RadioButton rb1;
    RadioButton rb2;
    RadioButton rb3;
    RadioButton rb4;
    TextView tv;
    View view = convertView;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.demo10, null);
        }
    }

    tv = (TextView) view.findViewById(R.id.tv);
    tv.setText(q[position]);

    rg = (RadioGroup) view.findViewById(R.id.rg);
    rb1 = (RadioButton) view.findViewById(R.id.rb1);
    rb2 = (RadioButton) view.findViewById(R.id.rb2);
    rb3 = (RadioButton) view.findViewById(R.id.rb3);
    rb4 = (RadioButton) view.findViewById(R.id.rb4);
    rb1.setText(a1[position]);
    rb2.setText(a2[position]);
    rb3.setText(a3[position]);
    rb4.setText(a4[position]);

    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rb = group.findViewById(checkedId);
            switch (checkedId){
                case R.id.rb1:
                    callback.RadioButton1(rb.getText().toString());
                    break;
                case R.id.rb2:
                    callback.RadioButton2(rb.getText().toString());
                    break;
                case R.id.rb3:
                    callback.RadioButton3(rb.getText().toString());
                    break;
                case R.id.rb4:
                    callback.RadioButton4(rb.getText().toString());
                    break;

            }
        }
    });
    return view;
}
}

监听器界面:---------

public interface Listener {

public void RadioButton1(String text);

public void RadioButton2(String text);

public void RadioButton3(String text);

public void RadioButton4(String text);

}

demo11.xml:--------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>

demo10.xml:-----

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Question"
    android:layout_marginStart="5dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/tv"/>

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rg"
    android:checkedButton="@+id/sound" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb1"
        android:text="1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb2"
        android:text="2" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb3"
        android:text="3" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb4"
        android:text="4" />

</RadioGroup>

</LinearLayout>

答案 1 :(得分:0)

你需要为&#34; OnCheckedChanged&#34;添加一个监听器。并在听众内部展示吐司。

此侦听器应添加到适配器的public View getView(int position, View convertView, ViewGroup parent)方法中。

这应该有所帮助:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = LayoutInflater.from(context).inflate(R.layout.radiobutton_list_view_item, parent, false);

    final RadioButton rbt = convertView.findViewById(R.id.rbtOption);

    /*
    Here you do whatever you need (setting radiobutton's name, tag, etc.)
    */

    rbt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                Toast.makeText(context, "Selected radiobutton " + rbt.getTag(), Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}