使用android中的类将数据绑定到listview

Posted

技术标签:

【中文标题】使用android中的类将数据绑定到listview【英文标题】:bind data to listview using class in android 【发布时间】:2014-05-07 19:36:45 【问题描述】:

嘿,我想显示列表视图的每个人

ackage com.example.ewa;

import java.util.ArrayList;
import java.util.Arrays;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity 

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


        ListView lst = (ListView) findViewById(R.id.listView1);
        tesst obj = new tesst();
        tesst[] ooo = new tesst[100];
        for(int i=0;i<ooo.length-1;i++)
        
            ooo[i]= new tesst();
        
        ooo[0].abss="Mercury";
        ooo[1].abss="Venus";
        String[] ppp= new String[1];
        for(int i=0;i<=1;i++)
        
            ppp[i]=(String)ooo[i].abss;
        
        String[] planets = new String[]  "Mercury", "Venus", "Earth", "Mars",
                "Jupiter", "Saturn", "Uranus", "Neptune" ;
        //ArrayList<String> planetList = new ArrayList<String>();
        //planetList.addAll(Arrays.asList(ppp));

        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
                R.layout.simplerow, R.id.textView1, ppp);

        listAdapter.add(ppp[0]);
        listAdapter.add(ppp[1]);

        // Add more planets. If you passed a String[] instead of a List<String>
        // into the ArrayAdapter constructor, you must not add more items.
        // Otherwise an exception will occur.


        // Set the ArrayAdapter as the ListView's adapter.
        lst.setAdapter(listAdapter);

而test是一个类

和错误

03-28 06:51:38.607: D/dalvikvm(1083): GC_FOR_ALLOC freed 39K, 4% free 2945K/3052K, paused 63ms, total 67ms
03-28 06:51:38.617: I/dalvikvm-heap(1083): Grow heap (frag case) to 3.545MB for 635812-byte allocation
03-28 06:51:38.687: D/dalvikvm(1083): GC_FOR_ALLOC freed 2K, 4% free 3563K/3676K, paused 61ms, total 61ms
03-28 06:51:39.067: D/AndroidRuntime(1083): Shutting down VM
03-28 06:51:39.067: W/dalvikvm(1083): threadid=1: thread exiting with uncaught exception (group=0xb3a37b90)
03-28 06:51:39.107: E/AndroidRuntime(1083): FATAL EXCEPTION: main
03-28 06:51:39.107: E/AndroidRuntime(1083): Process: com.example.ewa, PID: 1083
03-28 06:51:39.107: E/AndroidRuntime(1083): java.lang.NullPointerException
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.ListView.onMeasure(ListView.java:1175)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.View.measure(View.java:16458)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.View.measure(View.java:16458)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.View.measure(View.java:16458)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.View.measure(View.java:16458)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.View.measure(View.java:16458)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.os.Handler.handleCallback(Handler.java:733)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.os.Looper.loop(Looper.java:137)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 06:51:39.107: E/AndroidRuntime(1083):     at dalvik.system.NativeStart.main(Native Method)
03-28 06:51:42.627: I/Process(1083): Sending signal. PID: 1083 SIG: 9
03-28 06:54:51.637: D/dalvikvm(1109): GC_FOR_ALLOC freed 42K, 4% free 2945K/3056K, paused 137ms, total 141ms
03-28 06:54:51.657: I/dalvikvm-heap(1109): Grow heap (frag case) to 3.545MB for 635812-byte allocation
03-28 06:54:51.807: D/dalvikvm(1109): GC_FOR_ALLOC freed 2K, 4% free 3563K/3680K, paused 144ms, total 144ms
03-28 06:54:52.507: D/AndroidRuntime(1109): Shutting down VM
03-28 06:54:52.507: W/dalvikvm(1109): threadid=1: thread exiting with uncaught exception (group=0xb3a37b90)
03-28 06:54:52.607: E/AndroidRuntime(1109): FATAL EXCEPTION: main
03-28 06:54:52.607: E/AndroidRuntime(1109): Process: com.example.ewa, PID: 1109
03-28 06:54:52.607: E/AndroidRuntime(1109): java.lang.NullPointerException
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.ListView.onMeasure(ListView.java:1175)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.View.measure(View.java:16458)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.View.measure(View.java:16458)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.View.measure(View.java:16458)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.View.measure(View.java:16458)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.View.measure(View.java:16458)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.os.Handler.handleCallback(Handler.java:733)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.os.Looper.loop(Looper.java:137)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 06:54:52.607: E/AndroidRuntime(1109):     at dalvik.system.NativeStart.main(Native Method)
03-28 06:55:13.607: D/dalvikvm(1152): GC_FOR_ALLOC freed 46K, 4% free 2945K/3060K, paused 105ms, total 116ms
03-28 06:55:13.617: I/dalvikvm-heap(1152): Grow heap (frag case) to 3.545MB for 635812-byte allocation
03-28 06:55:13.917: D/dalvikvm(1152): GC_FOR_ALLOC freed 2K, 4% free 3563K/3684K, paused 145ms, total 145ms
03-28 06:55:15.007: I/Choreographer(1152): Skipped 153 frames!  The application may be doing too much work on its main thread.
03-28 06:55:15.217: D/gralloc_goldfish(1152): Emulator without GPU emulation detected.
03-28 06:55:18.467: I/Choreographer(1152): Skipped 37 frames!  The application may be doing too much work on its main thread.
03-28 06:55:19.507: I/Choreographer(1152): Skipped 43 frames!  The application may be doing too much work on its main thread.
03-28 06:59:27.297: D/dalvikvm(1200): GC_FOR_ALLOC freed 50K, 4% free 2945K/3064K, paused 119ms, total 120ms
03-28 06:59:27.337: I/dalvikvm-heap(1200): Grow heap (frag case) to 3.545MB for 635812-byte allocation
03-28 06:59:27.627: D/dalvikvm(1200): GC_FOR_ALLOC freed 0K, 4% free 3566K/3688K, paused 61ms, total 61ms
03-28 06:59:27.787: D/AndroidRuntime(1200): Shutting down VM
03-28 06:59:27.787: W/dalvikvm(1200): threadid=1: thread exiting with uncaught exception (group=0xb3a37b90)
03-28 06:59:27.857: E/AndroidRuntime(1200): FATAL EXCEPTION: main
03-28 06:59:27.857: E/AndroidRuntime(1200): Process: com.example.ewa, PID: 1200
03-28 06:59:27.857: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.ewa/com.example.ewa.MainActivity: java.lang.UnsupportedOperationException
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.os.Looper.loop(Looper.java:137)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at dalvik.system.NativeStart.main(Native Method)
03-28 06:59:27.857: E/AndroidRuntime(1200): Caused by: java.lang.UnsupportedOperationException
03-28 06:59:27.857: E/AndroidRuntime(1200):     at java.util.AbstractList.add(AbstractList.java:404)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at java.util.AbstractList.add(AbstractList.java:425)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at com.example.ewa.MainActivity.onCreate(MainActivity.java:46)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.Activity.performCreate(Activity.java:5243)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 06:59:27.857: E/AndroidRuntime(1200):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-28 06:59:27.857: E/AndroidRuntime(1200):     ... 11 more
03-28 06:59:32.717: I/Process(1200): Sending signal. PID: 1200 SIG: 9
03-28 07:00:27.967: D/dalvikvm(1254): GC_FOR_ALLOC freed 53K, 4% free 2945K/3068K, paused 64ms, total 67ms
03-28 07:00:27.987: I/dalvikvm-heap(1254): Grow heap (frag case) to 3.545MB for 635812-byte allocation
03-28 07:00:28.057: D/dalvikvm(1254): GC_FOR_ALLOC freed 2K, 4% free 3563K/3692K, paused 68ms, total 68ms
03-28 07:00:28.217: D/AndroidRuntime(1254): Shutting down VM
03-28 07:00:28.217: W/dalvikvm(1254): threadid=1: thread exiting with uncaught exception (group=0xb3a37b90)
03-28 07:00:28.257: E/AndroidRuntime(1254): FATAL EXCEPTION: main
03-28 07:00:28.257: E/AndroidRuntime(1254): Process: com.example.ewa, PID: 1254
03-28 07:00:28.257: E/AndroidRuntime(1254): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.ewa/com.example.ewa.MainActivity: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.os.Looper.loop(Looper.java:137)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at dalvik.system.NativeStart.main(Native Method)
03-28 07:00:28.257: E/AndroidRuntime(1254): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-28 07:00:28.257: E/AndroidRuntime(1254):     at com.example.ewa.MainActivity.onCreate(MainActivity.java:36)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.Activity.performCreate(Activity.java:5243)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 07:00:28.257: E/AndroidRuntime(1254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-28 07:00:28.257: E/AndroidRuntime(1254):     ... 11 more
03-28 07:00:39.477: D/dalvikvm(1288): GC_FOR_ALLOC freed 57K, 5% free 2945K/3072K, paused 94ms, total 98ms
03-28 07:00:39.487: I/dalvikvm-heap(1288): Grow heap (frag case) to 3.545MB for 635812-byte allocation
03-28 07:00:39.547: D/dalvikvm(1288): GC_FOR_ALLOC freed 2K, 4% free 3563K/3696K, paused 56ms, total 56ms
03-28 07:00:39.747: D/AndroidRuntime(1288): Shutting down VM
03-28 07:00:39.747: W/dalvikvm(1288): threadid=1: thread exiting with uncaught exception (group=0xb3a37b90)
03-28 07:00:39.767: E/AndroidRuntime(1288): FATAL EXCEPTION: main
03-28 07:00:39.767: E/AndroidRuntime(1288): Process: com.example.ewa, PID: 1288
03-28 07:00:39.767: E/AndroidRuntime(1288): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.ewa/com.example.ewa.MainActivity: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.os.Looper.loop(Looper.java:137)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at dalvik.system.NativeStart.main(Native Method)
03-28 07:00:39.767: E/AndroidRuntime(1288): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
03-28 07:00:39.767: E/AndroidRuntime(1288):     at com.example.ewa.MainActivity.onCreate(MainActivity.java:36)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.Activity.performCreate(Activity.java:5243)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 07:00:39.767: E/AndroidRuntime(1288):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-28 07:00:39.767: E/AndroidRuntime(1288):     ... 11 more
03-28 07:05:37.197: I/Process(1288): Sending signal. PID: 1288 SIG: 9

如何解决,请帮助我

【问题讨论】:

【参考方案1】:

您好,我发现这是什么问题。你的 are 数组声明是错误的。

更改此行:

String[] ppp= new String[2];

原因,在你的代码中

 String[] ppp= new String[1];
    for(int i=0;i<=1;i++)
    
        ppp[i]=(String)ooo[i].abss;
    

您正在创建大小为 1 的 ppp 数组。这意味着 ppp[0] 仅可用。 但在 for 循环中,您尝试访问 ppp[1]

所以 ArrayIndexOutOfBoundException 发生了。

【讨论】:

请将此标记为答案,然后其他人将使用此。谢谢【参考方案2】:

你读过这些话吗?

// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.

问题是您正在传递字符串数组 (ppp[])。所以你不能在创建适配器后添加更多项目。

删除以下行,您的错误就会消失。

listAdapter.add(ppp[0]);
listAdapter.add(ppp[1]);

无需显式添加这些项目。

【讨论】:

prakash 我的朋友,如果我删除此行,那么对输出没有影响 更改字符串[] ppp=新字符串[1]; to String[] ppp= new String[2];检查我的另一个答案【参考方案3】:
String[] ppp = ...
List<string> list = new List<string>(ppp);

listAdapter.add(list.get(0));
listAdapter.add(list.get(1));

这是你想要的吗?

【讨论】:

不,我有类测试和类有成员 abss.. 使用类作为数组并将值分配给类的数组,但问题是我想将类数组转换为字符串数组,字符串数组用于绑定列表视图

以上是关于使用android中的类将数据绑定到listview的主要内容,如果未能解决你的问题,请参考以下文章

vue组件class绑定

arcgis python 使用光标和内存中的要素类将数据加载到要素集

Android Studio中未生成MainActivityBinding文件

Android:将数据库中的数据绑定到 ListView 中的 CheckBox?

如何使用 XmlConvert 类将 byte[] 转换为字符串? [复制]

WPF 绑定到代码后面的类成员