Android:切换到ListActivity时无法设置setContentView

Yan*_*ang 3 layout android listactivity

[更新]我收到错误,上面写着"你的内容必须有一个ListView,其id属性是'android.R.id.list'".我的xml中似乎没有任何东西是ListView.但这需要吗?

这是关于我之前的问题android的后续问题 :我应该使用哪个视图来显示文本和图像?

我阅读了有关为LinearLayout创建ListView的文章.但是,当我将"extends Activity"更改为"扩展ListActivity"时,我的以下代码在setContentView()函数中失败,任何想法为什么?

private TextView mSelection;
//private ImageView mImages;
static final String[] keywords = new String[]{"China", "Japan", "USA", "Canada"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.contactLayout);

    mSelection = (TextView)findViewById(R.id.ContactNames);
    ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.contactlayout, R.id.ContactNames,keywords);
    setListAdapter(adapter);
    }
Run Code Online (Sandbox Code Playgroud)

我的布局来自这篇文章:http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"

android:padding="6dip">

<ImageView
    android:id="@+id/icon"

    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"

    android:src="@drawable/icon" />

<LinearLayout
    android:orientation="vertical"

    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/ContactNames"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"

        android:gravity="center_vertical"
        android:text="My Application" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" 

        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Simple application that shows how to use RelativeLayout" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Ste*_*ley 11

我想你误解了我在上一个问题中向你展示的其他帖子.他们解释了如何为列表中的每一行使用自定义布局,而不是如何为活动定义整个布局文件.你需要这样的东西:

(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:id="@android:id/list">
</ListView>
Run Code Online (Sandbox Code Playgroud)

请注意非常重要的一行android:id="@android:id/list".你必须在ListView中拥有它,因为它告诉Android你的列表在哪里.如果您的背景不是黑色,则cacheColorHint非常有用 - 有关详细信息,请参阅此帖子.

通过上述行,您可以为活动提供一个可以正确识别的列表.这是一个基本的例子:

public class TestProject extends ListActivity {

    final static String[] ITEMS = {"blah", "floop", "gnarlp", "stuff"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.listrow, R.id.textview, ITEMS);
        setListAdapter(adapter);
    }
}
Run Code Online (Sandbox Code Playgroud)

那么listrow布局就是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的布局.如果你想要更复杂的东西,你必须使用BaseAdapter进行更改,因为它会getView(...)在每行之前给你调用.您可以根据每行的内容使用不同的布局.但是,BaseAdapter在您第一次尝试时看起来很可怕,所以请注意!:)