ListView与标头不起作用

时间:2012-12-04 17:59:39

标签: android android-listview

我有一个ListView工作正常,但我希望我的标题位于顶部,所以我创建了一个新的布局,我将标题和ListView放在一起。现在我的力量已经接近了,我真的不知道它有什么问题。

public class AndroidListViewActivity extends ListActivity {

    String value;
    // Places Listview
        ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_item3);

        String[] restaurant = getResources().getStringArray(R.array.restaurant);
        String[] bar = getResources().getStringArray(R.array.bar);
        String[] gas = getResources().getStringArray(R.array.gas);
        String[] help = getResources().getStringArray(R.array.help);
        String[] hotel = getResources().getStringArray(R.array.hotel);
        String[] shopping = getResources().getStringArray(R.array.shopping);
        String[] bank = getResources().getStringArray(R.array.bank);
        String[] bus = getResources().getStringArray(R.array.bus);
        String[] film = getResources().getStringArray(R.array.film);


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
         value = extras.getString("categoriename");
        }

        // storing string resources into Array
        if(value.equals("restaurant")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, restaurant));
        }if(value.equals("bar")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bar));
        }if(value.equals("gas")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, gas));
        }if(value.equals("film")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, film));
        }if(value.equals("help")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, help));
        }if(value.equals("bus")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bus));
        }if(value.equals("bank")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, bank));
        }if(value.equals("shopping")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, shopping));
        }if(value.equals("hotel")){
             // Binding resources Array to ListAdapter
            this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2, R.id.label, hotel));
        }





        lv = (ListView) findViewById(R.id.list3);

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

              // selected item
              String product = ((TextView) view).getText().toString();

              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), MainActivity.class);
              // sending data to new activity
              i.putExtra("thetext", product);
              startActivity(i);

          }
        });
    }

这是我的list_item2:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="@drawable/list_selector"
    android:padding="10dip"
    android:textSize="16dip"
     android:layout_below="@+id/imageButton2" 
    android:textStyle="bold" >
    </TextView>

这是我的list_item3:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF"
    android:id="@+id/mainLayout"
    > 
     <ImageView
        android:id="@+id/imageButton2"
        android:layout_width="fill_parent"
        android:layout_height="52dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/header"
        android:src="@drawable/list_header" />
    <ImageButton
        android:id="@+id/imageButtonSearch"
        android:layout_width="60dip"
        android:layout_height="45dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/header"
        android:src="@drawable/search" 
        android:background="@android:color/transparent" 
        />
    <ImageView
        android:id="@+id/imageButton3"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/titel" />

    <ListView
        android:id="@+id/list3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2" />

    </RelativeLayout>

这是logcat:

    12-04 18:29:49.886: E/AndroidRuntime(371): FATAL EXCEPTION: main
12-04 18:29:49.886: E/AndroidRuntime(371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laurenswuyts.find.it/com.laurenswuyts.find.it.AndroidListViewActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.os.Looper.loop(Looper.java:130)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 18:29:49.886: E/AndroidRuntime(371):  at java.lang.reflect.Method.invokeNative(Native Method)
12-04 18:29:49.886: E/AndroidRuntime(371):  at java.lang.reflect.Method.invoke(Method.java:507)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 18:29:49.886: E/AndroidRuntime(371):  at dalvik.system.NativeStart.main(Native Method)
12-04 18:29:49.886: E/AndroidRuntime(371): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.Activity.setContentView(Activity.java:1657)
12-04 18:29:49.886: E/AndroidRuntime(371):  at com.laurenswuyts.find.it.AndroidListViewActivity.onCreate(AndroidListViewActivity.java:23)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 18:29:49.886: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 18:29:49.886: E/AndroidRuntime(371):  ... 11 more

1 个答案:

答案 0 :(得分:2)

ListActivity有一种特殊行为,您不会在代码中考虑这些行为。如果您使用R.layout.list_item3布局文件作为ListActivity的内容视图,则该布局文件中的ListView必须的ID为android:id="@android:id/list" (因此ListActivity将知道从自定义布局中使用哪个ListView窗口小部件。因此,在R.layout.list_item3中修改ListView项,如下所示:

//....
 <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton2" />
// ...

在您的代码中,您无需查找ListView只需使用:

lv = getListView();