【发布时间】:2016-03-09 17:55:08
【问题描述】:
我正在尝试实现一个代码,其中使用数组适配器将线性布局列表插入到列表视图中。每个线性布局包含两个子视图。这是xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="@+id/list_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
现在我在片段中创建了一个这样的代码 sn-p。
final String[] my_page_items = getResources().getStringArray(R.array.my_page_items);
int[] my_page_icons = {
R.mipmap.ic_about,
R.mipmap.ic_app_settings,
R.mipmap.ic_account_settings,
R.mipmap.ic_logout
};
LinearLayout[] items = new LinearLayout[my_page_items.length];
TextView[] labels = new TextView[my_page_items.length];
ImageView[] icons = new ImageView[my_page_icons.length];
for(int i=0; i<my_page_icons.length; i++) {
items[i] = (LinearLayout) LayoutInflater.from(getActivity().getApplication()).inflate(R.layout.list_item, null);
labels[i] = (TextView) items[i].findViewById(R.id.list_label);
icons[i] = (ImageView) items[i].findViewById(R.id.list_icon);
labels[i].setText(my_page_items[i]);
icons[i].setBackground(getResources().getDrawable(my_page_icons[i]));
}
setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.list_item, items));
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
});
当我运行这段代码时,我在 logcat 中得到以下内容。
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
这个问题应该如何解决?
【问题讨论】:
标签: android android-layout android-listview android-arrayadapter illegalstateexception