Android: Failed to setContentView when switching to ListActivity

[update] I got an error saying: “Your content should have a ListView with id id and“ android.R.id.list. ”Apparently nothing in my XML is a ListView. But is this necessary?

This is the next question from my previous android question : what kind should I use to display text and image?

I read an article on creating a ListView for LinearLayout. However, my following code did not work in the setContentView () function when I changed the "extends Activity" to "extends ListActivity", any idea why?

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);
    }

My layout from this article: 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>

+3
3

, , . , , . - :

(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>

android:id="@android:id/list". ListView , Android, . CacheColorHint , - .

:

<?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>

. - , BaseAdapter, getView(...) . . , BaseAdapter , , !:)

+11

, ListActivity, ListView, android.R.list .

ListView , , .

+1

() ListView . - setContentView(). , , Android "" , , ListView .

( ) Activity, XML Android, , , ListActivity. , , , , , ListView .

0
source

Source: https://habr.com/ru/post/1739233/


All Articles