尝试使用自定义 ArrayAdapter 类将数组从 arrays.xml 获取到 ListView 中。怎么了?

发布于 2024-12-19 06:09:56 字数 883 浏览 0 评论 0原文

谁能告诉我我做错了什么? Eclipse 没有给我任何错误或警告,但当我运行模拟器时,我的 ListView 没有被填充。非常感谢任何帮助。谢谢。

这是在 onCreate 内部:

ListView bedList = (ListView) findViewById(R.id.refineBeds);
bedList.setVisibility(View.VISIBLE); 
bedList.setAdapter(new MArrayAdapter(this, R.array.beds));
bedList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

这是我尝试用作适配器的自定义内部类:

private static class MArrayAdapter extends ArrayAdapter<String> {
    public MArrayAdapter(final Context context, final int objects) {
    super(context, R.layout.item_single, objects);
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    final CheckedTextView view = (CheckedTextView)
    super.getView(position, convertView, parent);
    view.setChecked(position == 2); return view;
    }
}

Can anyone tell me what I'm doing wrong? It Eclipse isn't giving me any errors or warnings, but my ListView isn't being populated when i run the emulator. Any help is much appreciated. Thanks.

This is inside onCreate:

ListView bedList = (ListView) findViewById(R.id.refineBeds);
bedList.setVisibility(View.VISIBLE); 
bedList.setAdapter(new MArrayAdapter(this, R.array.beds));
bedList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

This is a custom inner class I'm trying to use as my adapter:

private static class MArrayAdapter extends ArrayAdapter<String> {
    public MArrayAdapter(final Context context, final int objects) {
    super(context, R.layout.item_single, objects);
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    final CheckedTextView view = (CheckedTextView)
    super.getView(position, convertView, parent);
    view.setChecked(position == 2); return view;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-12-26 06:09:56

首先尝试不使用这样的自定义布局,然后最终可以使用自己的布局进行调整:

...

String[] bedsArray = getResources().getStringArray(R.array.beds);

bedList.setListAdapter(new ArrayAdapter<String>(
                           this,
                           android.R.layout.simple_expandable_list_item_1,
                           bedsArray));

...

Try first without a cusom layout like this, then eventually you can adjust it using your own layout:

...

String[] bedsArray = getResources().getStringArray(R.array.beds);

bedList.setListAdapter(new ArrayAdapter<String>(
                           this,
                           android.R.layout.simple_expandable_list_item_1,
                           bedsArray));

...
盛夏已如深秋| 2024-12-26 06:09:56

问题是下面的这一行

new MArrayAdapter(this, R.array.beds)

这对应于以下定义

public ArrayAdapter (Context context, int textViewResourceId)

尝试从 R.array.beds 创建字符串元素列表并使用以下构造函数

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

Problem is this line below

new MArrayAdapter(this, R.array.beds)

This corresponds to following definition

public ArrayAdapter (Context context, int textViewResourceId)

Try creating a List of string elements from R.array.beds and use the following constructor

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文