简体   繁体   中英

ListActivity Does Not Show Any Entries In The ListView

This seems to be a problem that is encountered often, but couldn't find a solution for my situation among those that are offered.

So I have an Activity that extends a ListActivity and the following code within it:

super.onCreate(savedInstanceState);



        db = SQLiteDatabase.openDatabase(playersDatabaseName, null, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.OPEN_READWRITE);
        SQLStatement = "create table if not exists Players_And_Scores(_id integer primary key autoincrement, Player String, Score String);";
        db.execSQL(SQLStatement);


        cv.put("Player", WelcomeActivity.playersName);
        cv.put("Score", GameActivity.currentScore);
        try
        {
            db.insertOrThrow("Players_And_Scores", null, cv);
        }
        catch (Exception e)
        {
            Log.i("SQLiteException", cv.toString() + " is there already");
        }

        c = db.query("Players_And_Scores", null, null, null, null, null, null, null);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,
                                                              c, new String[] {"Player", "Score"}, new int[] {android.R.id.text1}, 0);

        this.setListAdapter(adapter);

This does set up a listview and every time the entry is added a new row is added into a database a new line is added to a listwiev. However all of the lines are empty. So it recognizes the number of lines, but not the contents.

I was thinking if I should add something to xml so I've been playing with it without result. Here is what I have currently for xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- the android:id is important -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

Any ideas would be greatly appreciated.

EDIT here is the logcat:

  04-23 03:05:16.083    3332-3332/statesgame.app D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
    04-23 03:05:16.193    3332-3332/statesgame.app I/can read﹕ true
    04-23 03:05:16.193    3332-3332/statesgame.app I/this﹕ State                   Capital
    04-23 03:05:16.193    3332-3332/statesgame.app I/this﹕ ----------------        ---------------
    04-23 03:05:16.193    3332-3332/statesgame.app I/this﹕ Alabama                 Montgomery
   State=Wyoming
    04-23 03:05:16.473    3332-3332/statesgame.app I/SQLiteException﹕ Capital=Cheyenne State=Wyoming is there already
    04-23 03:05:16.483    3332-3332/statesgame.app I/db﹕ []
    04-23 03:05:16.513    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 485K, 16% free 3261K/3856K, paused 5ms, total 13ms
    04-23 03:05:16.533    3332-3332/statesgame.app D/﹕ HostConnection::get() New Host Connection established 0xb88f79f0, tid 3332
    04-23 03:05:16.573    3332-3332/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:05:16.573    3332-3332/statesgame.app D/OpenGLRenderer﹕ Enabling debug mode 0
    04-23 03:05:17.453    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 52K, 15% free 3298K/3856K, paused 3ms, total 4ms
    04-23 03:05:17.453    3332-3332/statesgame.app I/enteract﹕ act
    04-23 03:05:17.533    3332-3332/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:05:18.813    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:18.813    3332-3332/statesgame.app I/cursorEntirs﹕ 3
    04-23 03:05:18.813    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.143    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.143    3332-3332/statesgame.app I/cursorEntirs﹕ 33
    04-23 03:05:19.143    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.313    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.313    3332-3332/statesgame.app I/cursorEntirs﹕ 34
    04-23 03:05:19.313    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.483    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.483    3332-3332/statesgame.app I/cursorEntirs﹕ 5
    04-23 03:05:19.483    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.623    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.623    3332-3332/statesgame.app I/cursorEntirs﹕ 2
    04-23 03:05:19.623    3332-3332/statesgame.app I/cCount﹕ 50
    04-23 03:05:19.743    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:19.893    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.063    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.213    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.533    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.763    3332-3332/statesgame.app I/buttn﹕ Click
    04-23 03:05:20.793    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 30K, 13% free 3427K/3896K, paused 2ms, total 3ms
    04-23 03:05:20.793    3332-3332/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 5K, 12% free 3520K/3996K, paused 2ms, total 2ms
    04-23 03:05:20.803    3332-3332/statesgame.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.618MB for 1127532-byte allocation
    04-23 03:05:20.823    3332-3341/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 10% free 4621K/5100K, paused 21ms, total 21ms
    04-23 03:05:20.873    3332-3332/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:09:38.607    3439-3439/statesgame.app D/AndroidRuntime﹕ Shutting down VM
    04-23 03:09:38.607    3439-3439/statesgame.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2ce5b20)
    04-23 03:09:38.607    3439-3439/statesgame.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: statesgame.app, PID: 3439
        java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.IllegalStateException: Unable to get package info for statesgame.app; is package not installed?
                at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
                at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5017)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.IllegalStateException: Unable to get package info for statesgame.app; is package not installed?
                at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:376)
                at android.app.LoadedApk.getClassLoader(LoadedApk.java:329)
                at android.app.LoadedApk.makeApplication(LoadedApk.java:508)
                at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
                at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5017)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                at dalvik.system.NativeStart.main(Native Method)
    04-23 03:09:39.737    3468-3468/statesgame.app D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
    04-23 03:09:39.787    3468-3468/statesgame.app I/can read﹕ true
    04-23 03:09:39.797    3468-3468/statesgame.app I/this﹕ State                   Capital
    04-23 03:09:39.797    3468-3468/statesgame.app I/this﹕ ----------------        ---------------
    04-23 03:09:39.797    3468-3468/statesgame.app I/this﹕ Alabama                 Montgomery
    04-23 03:09:39.817    3468-3468/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 109K, 7% free 3230K/3448K, paused 18ms, total 19ms
    04-23 03:09:39.827    3468-3468/statesgame.app I/SQLiteException﹕ Capital=Montgomery State=Alabama is there already

    04-23 03:10:05.507    3468-3468/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 75K, 12% free 3428K/3892K, paused 3ms, total 3ms
    04-23 03:10:05.507    3468-3468/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 8K, 12% free 3518K/3992K, paused 2ms, total 2ms
    04-23 03:10:05.507    3468-3468/statesgame.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.616MB for 1127532-byte allocation
    04-23 03:10:05.527    3468-3477/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 10% free 4619K/5096K, paused 13ms, total 13ms
    04-23 03:10:05.577    3468-3468/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:18:27.315    3575-3575/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    04-23 03:18:29.785    3575-3575/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 276K, 11% free 3456K/3844K, paused 3ms, total 3ms
    04-23 03:18:29.785    3575-3575/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed 36K, 11% free 3518K/3944K, paused 2ms, total 3ms
    04-23 03:18:29.785    3575-3575/statesgame.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.616MB for 1127532-byte allocation
    04-23 03:18:29.805    3575-3584/statesgame.app D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 9% free 4619K/5048K, paused 12ms, total 12ms
    04-23 03:18:29.875    3575-3575/statesgame.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented

Edit Added manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="statesgame.app" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="statesgame.app.WelcomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="statesgame.app.GameActivity"
            android:label="@string/title_activity_game" >
        </activity>
        <activity
            android:name="statesgame.app.ScoresActivity"
            android:label="@string/title_activity_scores" >
        </activity>
    </application>

</manifest>

我的活动显示为

I start my activity this way:

 Intent intent = new Intent(getApplicationContext(), ScoresActivity.class);
        startActivity(intent);

Try out as below:

      Cursor cursor = db.query("Players_And_Scores", new String[] 
     {"Player","Score"}, null, null, null);
       startManagingCursor(cursor);

        // THE DESIRED COLUMNS TO BE BOUND
        String[] columns = new String[] { "Player", "Score"};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_expandable_list_item_2,
         cursor , columns, new int[] {android.R.id.text1,android.R.id.text2}, 0);

Hope this will work for you.

I found the solution. Wierdly enough, I simply changed my cursor object to this:

c = db.query("Players_And_Scores", new String[]{"_id", "Player", "Score"}, null, null, null, null, null, null);

Adding all the rows of my database explicitly solved the problem, but unfortunately only one entry "Players" gets displayed. In order to place more entries in a ListView row I will probably need to create a custom adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM