gpt4 book ai didi

java - 要在 ListView 中显示的项目列表,其中数据源是数组

转载 作者:行者123 更新时间:2023-12-01 13:43:16 26 4
gpt4 key购买 nike

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

<小时/>

我尝试了什么::

我在这里使用ArrayAdapter是因为我的数据源是一个数组

<小时/>

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
<小时/>

为什么会发生这种情况? ....我在显示数组时提到了数据源

我该如何解决这个问题?

最佳答案

ArrayAdapter requires the resource ID to be a TextView

这只是意味着,您必须提供一个带有 TextView 作为参数的布局。适配器会将此 TextView 绑定(bind)到您的数据。但您当前正在传递带有 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);

关于java - 要在 ListView 中显示的项目列表,其中数据源是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20525381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com