的ListView与含有的LinearLayout行 [英] ListView with rows containing a LinearLayout

查看:131
本文介绍了的ListView与含有的LinearLayout行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用ListView控件,它包含一个内置的LinearLayout行的列表。这是的LinearLayout组成的复选框,一个TextView的。行的布局被命名tasks_list_row.xml:

I'm trying to implement a list using the ListView, which contains rows built with a LinearLayout. This LinearLayout is composed of a checkbox and a textview. The layout of the row is named tasks_list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <CheckBox android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

然后从我的code初始化我名单是这样的:

Then from my code I initialize the list this way:

final String[] items_task = new String[] { "one",
                "two",
                "three" };

setListAdapter(new ArrayAdapter<String>(this, R.layout.tasks_list_row, items_task));

但后来我得到一个错误:
ArrayAdapter - 你必须提供一个资源ID为一个TextView

我尝试指定TextView的ID是这样的:

I then try to specify the TextView id this way:

setListAdapter(new ArrayAdapter<String>(this, R.id.text1, R.layout.tasks_list_row, items_task));

但我得到一个未找到资源错误(R.id.text1存在于R.java文件)。

But I get a Resource not found error (R.id.text1 exists in the R.java file).

我应该怎么办?

推荐答案

在这种情况下,你需要使用android.R.id.text1,因为你引用内置的Andr​​oid的ID(@android:ID是线索)。

In this case, you need to use android.R.id.text1, as you're referencing the built in android ID (@android:id was the clue).

setListAdapter(new ArrayAdapter<String>(this, R.layout.tasks_list_row, android.R.id.text1, items_task));

有没有太大的优势,这样做,除非你使用内置的布局,比如android.R.layout.two_line_list_item

There isn't much advantage to doing this, unless you're using built in layouts, such as android.R.layout.two_line_list_item

这篇关于的ListView与含有的LinearLayout行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆