列表视图中显示的项目列表,其中数据源是数组

时间:2013-12-11 17:04:03

标签: java android android-layout

我有一个简单的任务,即在列表视图中显示项目列表


我尝试了什么 ::

我在ArrayAdapter使用这是因为我的data-source is an array


MainActivity.java

public class MainActivity extends Activity {

    ListView l;
    String[] days={ "Monday","Tuesday","wednesday"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        l=(ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.activity_main, days);
        l.setAdapter(adapter);

    }


}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

日志 ::

12-11 22:21:05.084: E/ArrayAdapter(277): You must supply a resource ID for a TextView
12-11 22:21:05.093: D/AndroidRuntime(277): Shutting down VM
12-11 22:21:05.093: W/dalvikvm(277): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-11 22:21:05.093: E/AndroidRuntime(277): Uncaught handler: thread main exiting due to uncaught exception
12-11 22:21:05.123: E/AndroidRuntime(277): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.AbsListView.obtainView(AbsListView.java:1274)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.ListView.onMeasure(ListView.java:1060)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.View.measure(View.java:7964)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:554)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:377)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.View.measure(View.java:7964)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.View.measure(View.java:7964)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.View.measure(View.java:7964)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.View.measure(View.java:7964)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.os.Looper.loop(Looper.java:123)
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 22:21:05.123: E/AndroidRuntime(277):  at java.lang.reflect.Method.invokeNative(Native Method)
12-11 22:21:05.123: E/AndroidRuntime(277):  at java.lang.reflect.Method.invoke(Method.java:521)
12-11 22:21:05.123: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 22:21:05.123: E/AndroidRuntime(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 22:21:05.123: E/AndroidRuntime(277):  at dalvik.system.NativeStart.main(Native Method)
12-11 22:21:05.123: E/AndroidRuntime(277): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
12-11 22:21:05.123: E/AndroidRuntime(277):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
12-11 22:21:05.123: E/AndroidRuntime(277):  ... 27 more
12-11 22:21:05.153: I/dalvikvm(277): threadid=7: reacting to signal 3
12-11 22:21:05.153: E/dalvikvm(277): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
12-11 22:24:32.194: E/ArrayAdapter(303): You must supply a resource ID for a TextView
12-11 22:24:32.194: D/AndroidRuntime(303): Shutting down VM
12-11 22:24:32.194: W/dalvikvm(303): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-11 22:24:32.194: E/AndroidRuntime(303): Uncaught handler: thread main exiting due to uncaught exception
12-11 22:24:32.233: E/AndroidRuntime(303): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.AbsListView.obtainView(AbsListView.java:1274)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.ListView.onMeasure(ListView.java:1060)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.View.measure(View.java:7964)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:554)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:377)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.View.measure(View.java:7964)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.View.measure(View.java:7964)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.View.measure(View.java:7964)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.View.measure(View.java:7964)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.os.Looper.loop(Looper.java:123)
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 22:24:32.233: E/AndroidRuntime(303):  at java.lang.reflect.Method.invokeNative(Native Method)
12-11 22:24:32.233: E/AndroidRuntime(303):  at java.lang.reflect.Method.invoke(Method.java:521)
12-11 22:24:32.233: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 22:24:32.233: E/AndroidRuntime(303):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 22:24:32.233: E/AndroidRuntime(303):  at dalvik.system.NativeStart.main(Native Method)
12-11 22:24:32.233: E/AndroidRuntime(303): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
12-11 22:24:32.233: E/AndroidRuntime(303):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
12-11 22:24:32.233: E/AndroidRuntime(303):  ... 27 more
12-11 22:24:32.263: I/dalvikvm(303): threadid=7: reacting to signal 3
12-11 22:24:32.263: E/dalvikvm(303): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
12-11 22:29:32.305: I/Process(303): Sending signal. PID: 303 SIG: 9

为什么会这样? ....我在显示数组时提到了数据源

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

  

ArrayAdapter要求资源ID为TextView

这只是意味着您必须提供一个以TextView作为参数的布局。适配器会将此TextView绑定到您的数据。但是,您目前正在使用ListView传递布局。

所以改变这个:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, days);

到此:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, days);