【问题标题】:RadioButton adapter not sending String instead of actual RadioButton to the listViewRadioButton 适配器不向 listView 发送 String 而不是实际 RadioButton
【发布时间】:2018-06-27 09:55:53
【问题描述】:

编辑:通过使用答案中的一些链接,我刚刚制作了一个自定义适配器(实际上不到 10 行代码),现在一切正常。这是我制作的CustomAdapter,以防有人遇到类似问题。请注意,CustomAdapter 适用于任何扩展“视图”的类。

public class CustomAdapter extends BaseAdapter {

Context context;
List<View> views;
public CustomAdapter(Context context, List<View> views){
    this.context = context;
    this.views = views;
}

@Override
public int getCount() {
    return views.size();
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    return views.get(i);
}}

我做了一个简单的Activity,假设显示我传递给它的radioButtons 列表。为了进行测试,在将其传递给activity 之前,我只是尝试在activity 中创建一个新的RadioButton,将其添加到列表中,然后让适配器将其传递给listView

public class SpecialListActivity extends Activity {

    private ListView listView;
    private ArrayAdapter<RadioButton> adapter;

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

        List<RadioButton> list = new ArrayList<RadioButton>();

        RadioButton radioButton = new RadioButton(this);
        radioButton.setText("test");

        list.add(radioButton);

        listView = (ListView) findViewById(R.id.listView);

        adapter = new ArrayAdapter<RadioButton>(this,R.layout.message_row,list);

        listView.setAdapter(adapter);
    }
}

这是一个非常简单的 .xml 文件,只有一个 listView。

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

    </ListView>

</LinearLayout>

我预期的结果是单个 RadioButton 显示在列表中,但结果有点不同。

此行为的原因可能是什么?如何尝试使列表显示实际的单选按钮或任何其他 customView?

【问题讨论】:

  • 你的 R.layout.message_row 是什么
  • 老实说没有线索,它就在那里,当我用字符串尝试这个时,一切都奏效了,没想太多。
  • 然后使用可能有用的自定义适配器

标签: android listview radio-button android-custom-view android-adapter


【解决方案1】:

您已经在 List 和 ArrayAdapter 中定义了 RadioButton 数据类型,并且您正在传递 RadioButton 的对象,因此它将打印 RadioButton 对象。

如果您想传递 RadioButton 的文本,请使用以下代码。

public class SpecialListActivity extends Activity {

private ListView listView;
private ArrayAdapter<String> adapter;

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

    List<String> list = new ArrayList<String>();

    RadioButton radioButton = new RadioButton(this);
    radioButton.setText("test");

    list.add(radioButton.getText().toString());

    listView = (ListView) findViewById(R.id.listView);

    adapter = new ArrayAdapter<String>(this,R.layout.message_row,list);

    listView.setAdapter(adapter);
}
}

如果您想使用 RadioButton 创建自定义 ListView,请使用以下链接。 http://abhiandroid.com/ui/radiobutton-inside-listview.html

【讨论】:

    【解决方案2】:

    这不是它的工作方式。您必须使用带有字符串列表的ListView.CHOICE_MODE_SINGLE 来填充单选ListView。下面是一个示例,试试吧。

     String[] names = new String[] { "Canada", "USA", "Iceland", "Ireland"};
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setListAdapter(new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, names));
    

    【讨论】:

    • 我不确定我是否听懂了你的意思。我已经制作了一个获取字符串并将它们放入listView 的列表。您确定您的解决方案与customViews 有任何关系吗,我在您提供的解决方案中看不到任何类似的东西。我尝试运行代码,listView 不包含setListAdapter() 方法。
    • 您将 RadioButton 放入列表而不是 String 。您在上面添加的有点不同的结果是这个 RadioButton's 对象的十六进制等价物。
    猜你喜欢
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2016-05-01
    相关资源
    最近更新 更多