我正在尝试使用以下代码创建ListView
:
setListAdapter(new ArrayAdapter<String>(this, R.layout.settings_items, settingsLabels));
settingsLabels
为字符串数组且settings_items
为RelativeLayout
,包括TextView
和ImageView
。不幸的是,我无法将其解析为资源。我该如何正确实现这个?无论项目如何,ImageView
来源都是相同的。解决这个问题的最简单方法是什么?
EDIT settings_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/settings_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/image_leftarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:src="@android:drawable/ic_media_play" />
</RelativeLayout>
答案 0 :(得分:4)
更改用于创建适配器的代码,如下所示:
setListAdapter(new ArrayAdapter<String>(this, R.layout.settings_items, R.id.id_of_textView, settingsLabels));
答案 1 :(得分:3)
使用此构造函数:
setListAdapter(new ArrayAdapter<String>(this, R.layout.settings_items, R.id.id_of_textView, settingsLabels));
您使用的构造函数要求提供的布局为TextView
。以下是有关构造函数的文档的链接:http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int,%20java.util.List%3CT%3E%29