尝试使用自定义 ArrayAdapter 类将数组从 arrays.xml 获取到 ListView 中。怎么了?
谁能告诉我我做错了什么? 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先尝试不使用这样的自定义布局,然后最终可以使用自己的布局进行调整:
Try first without a cusom layout like this, then eventually you can adjust it using your own layout:
问题是下面的这一行
这对应于以下定义
尝试从 R.array.beds 创建字符串元素列表并使用以下构造函数
Problem is this line below
This corresponds to following definition
Try creating a List of string elements from R.array.beds and use the following constructor