0

下面是我的代码。我想让我的数据从一个活动读取到另一个活动,因为当我单击第一个表的一个项目时,它会根据外键向我显示该项目的相关数据。

这是我的代码

数据库类

package com.example.nearby_places;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database extends SQLiteOpenHelper {

    //database name & version number
    private static final String db_name = "nearby_place";
    private static final int db_version = 5;

    //tables
    private static final String table_placetypes = "placetypes";
    private static final String table_places = "table_places";

    //column names
    private static final String type_id = "type_id";
    private static final String type_name = "type_name";
    private static final String place_id = "place_id";
    private static final String place_name = "place_name";
    private static final String place_address = "place_address";
    private static final String place_contact = "place_contact";


    public Database(Context context) {
        super(context, db_name, null, db_version);
        // TODO Auto-generated constructor stub
    }

    // create table queries
    String create_table_placetypes = "CREATE TABLE IF NOT EXISTS " + table_placetypes + "("
            + type_id + " INTEGER PRIMARY KEY NOT NULL," + type_name + " TEXT" + ")";



    String create_table_places = "CREATE TABLE IF NOT EXISTS table_places (place_id INTEGER PRIMARY KEY NOT NULL, place_name TEXT, " +
            "place_address TEXT, place_contact TEXT, " +
            "type_id" +
            "FOREIGN KEY (type_id) REFERENCES table_placetypes(type_id))";



    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(create_table_placetypes);

        Log.d("creating", "placetypes created");
        db.execSQL(create_table_places);
        Log.d("creating", "places created");


    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + table_placetypes);
        db.execSQL("DROP TABLE IF EXISTS " + table_places);
        onCreate(db);

    }
    // add placetypes 
    void addplacetypes (placetypes pt) {

        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(type_name, pt.getTypename());

        db.insert(table_placetypes, null, values);
        db.close();

    }

     // Getting single placetypes
    placetypes getPlacetypes(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(table_placetypes, new String[] {type_id,
                type_name }, type_id + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        placetypes pt = new placetypes(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1));
        // return contact
        return pt;
    }

 // Getting All placetypes
    public List<placetypes> getAllPlacetypes() {
        List<placetypes> placetypesList = new ArrayList<placetypes>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + table_placetypes;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                placetypes pt = new placetypes();
                pt.setTypeid(Integer.parseInt(cursor.getString(0)));
                pt.setTypename(cursor.getString(1));


                String name = cursor.getString(1);

                //MainActivity.ArrayofName.add(name);
                // Adding contact to list
                placetypesList.add(pt);
            } while (cursor.moveToNext());
        }

        // return placetype list
        return placetypesList;
}

    // Getting placetypes Count
    public int getPlacetypesCount() {
        String countQuery = "SELECT  * FROM " + table_placetypes;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

    public void addplaces(places p) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(place_name, p.getPlace_name());
        values.put(place_address, p.getPlace_address());
        values.put(place_contact, p.getPlace_contact());
        values.put(type_id, p.getT_id());

        Log.d("Type ID", String.valueOf(p.getT_id()));
        db.insert(table_places, null, values);
        db.close();

    }

    places getPlaces(int pid) {

        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(table_places, new String[] {place_id, place_name, place_address, place_contact,type_id}, place_id + "=?", new String[] { String.valueOf(pid) } , null, null, null, null);

        if(cursor != null)
             cursor.moveToFirst();

        places p = new places(Integer.parseInt(cursor.getString(0)),
                Integer.parseInt(cursor.getString(1)),
                cursor.getString(2),
                cursor.getString(3),
                cursor.getString(4)
                );

        return p;

    }

    public List<places> getAllPlaces() {

        List<places> placeList = new ArrayList<places>();
        String selectQuery = "SELECT table_places.place_name FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id "; 

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()) {
            do{
            places p = new places();
            p.setT_id(cursor.getInt(0));
            p.setPlace_id(cursor.getInt(1));
            p.setPlace_name(cursor.getString(2));
            p.setPlace_address(cursor.getString(3));
            p.setPlace_contact(cursor.getString(4));

            /*String t_id = cursor.getString(4);
            String p_name = cursor.getString(2);
            String p_address = cursor.getString(3);
            String p_contact = cursor.getString(1);*/

            placeList.add(p);
            }while(cursor.moveToNext());
        }
        return placeList;
    }

    public int getPlaceCount () {

        String selectQuery = "SELECT * FROM " +table_places;
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(create_table_places, null);
        cursor.close();

        return cursor.getCount();
    }

}

日志猫

10-10 12:53:23.676: D/dalvikvm(7016): GC_FOR_ALLOC freed 52K, 10% free 2728K/3004K, paused 487ms, total 497ms
10-10 12:53:23.876: I/dalvikvm-heap(7016): Grow heap (frag case) to 3.952MB for 1127532-byte allocation
10-10 12:53:24.546: D/dalvikvm(7016): GC_FOR_ALLOC freed <1K, 7% free 3829K/4108K, paused 659ms, total 659ms
10-10 12:53:28.086: D/Reading:(7016): Reading all placetypes..
10-10 12:53:28.246: D/Name:(7016): Id: 1 ,Name: RESTURAUNTS
10-10 12:53:28.256: I/System.out(7016): Id: 1 ,Name: RESTURAUNTS
10-10 12:53:28.256: D/Name:(7016): Id: 2 ,Name: MALLS
10-10 12:53:28.256: I/System.out(7016): Id: 2 ,Name: MALLS
10-10 12:53:28.286: D/Name:(7016): Id: 3 ,Name: GAS STATIONS
10-10 12:53:28.286: I/System.out(7016): Id: 3 ,Name: GAS STATIONS
10-10 12:53:28.286: D/Name:(7016): Id: 4 ,Name: HOTELS
10-10 12:53:28.286: I/System.out(7016): Id: 4 ,Name: HOTELS
10-10 12:53:28.286: D/Name:(7016): Id: 5 ,Name: MOTELS
10-10 12:53:28.286: I/System.out(7016): Id: 5 ,Name: MOTELS
10-10 12:53:29.096: I/Choreographer(7016): Skipped 31 frames!  The application may be doing too much work on its main thread.
10-10 12:53:33.136: I/Choreographer(7016): Skipped 392 frames!  The application may be doing too much work on its main thread.
10-10 12:53:33.706: D/gralloc_goldfish(7016): Emulator without GPU emulation detected.
10-10 12:53:36.326: I/Choreographer(7016): Skipped 97 frames!  The application may be doing too much work on its main thread.
10-10 12:53:43.486: I/Choreographer(7016): Skipped 159 frames!  The application may be doing too much work on its main thread.
10-10 12:53:48.466: E/CursorWindow(7016): Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
10-10 12:53:48.586: W/dalvikvm(7016): threadid=1: thread exiting with uncaught exception (group=0xb0eff648)
10-10 12:53:48.586: D/AndroidRuntime(7016): Shutting down VM
10-10 12:53:49.096: E/AndroidRuntime(7016): FATAL EXCEPTION: main
10-10 12:53:49.096: E/AndroidRuntime(7016): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nearby_places/com.example.nearby_places.MainActivity2}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.os.Looper.loop(Looper.java:137)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at java.lang.reflect.Method.invoke(Method.java:525)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at dalvik.system.NativeStart.main(Native Method)
10-10 12:53:49.096: E/AndroidRuntime(7016): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.database.CursorWindow.nativeGetLong(Native Method)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.database.CursorWindow.getLong(CursorWindow.java:507)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.database.CursorWindow.getInt(CursorWindow.java:574)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at com.example.nearby_places.Database.getAllPlaces(Database.java:187)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at com.example.nearby_places.MainActivity2.onCreate(MainActivity2.java:33)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.Activity.performCreate(Activity.java:5133)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-10 12:53:49.096: E/AndroidRuntime(7016):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-10 12:53:49.096: E/AndroidRuntime(7016):     ... 11 more
10-10 12:58:55.861: I/Process(7016): Sending signal. PID: 7016 SIG: 9
10-10 12:59:11.491: D/dalvikvm(7597): GC_FOR_ALLOC freed 48K, 10% free 2728K/3000K, paused 567ms, total 576ms
10-10 12:59:11.611: I/dalvikvm-heap(7597): Grow heap (frag case) to 3.951MB for 1127532-byte allocation
10-10 12:59:12.101: D/dalvikvm(7597): GC_FOR_ALLOC freed 2K, 7% free 3826K/4104K, paused 480ms, total 481ms
10-10 12:59:14.931: E/CursorWindow(7597): Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
10-10 12:59:14.981: D/AndroidRuntime(7597): Shutting down VM
10-10 12:59:14.981: W/dalvikvm(7597): threadid=1: thread exiting with uncaught exception (group=0xb0eff648)
10-10 12:59:15.211: E/AndroidRuntime(7597): FATAL EXCEPTION: main
10-10 12:59:15.211: E/AndroidRuntime(7597): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nearby_places/com.example.nearby_places.MainActivity2}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.os.Looper.loop(Looper.java:137)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at java.lang.reflect.Method.invoke(Method.java:525)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at dalvik.system.NativeStart.main(Native Method)
10-10 12:59:15.211: E/AndroidRuntime(7597): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.database.CursorWindow.nativeGetLong(Native Method)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.database.CursorWindow.getLong(CursorWindow.java:507)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.database.CursorWindow.getInt(CursorWindow.java:574)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at com.example.nearby_places.Database.getAllPlaces(Database.java:187)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at com.example.nearby_places.MainActivity2.onCreate(MainActivity2.java:33)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.Activity.performCreate(Activity.java:5133)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-10 12:59:15.211: E/AndroidRuntime(7597):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-10 12:59:15.211: E/AndroidRuntime(7597):     ... 11 more
4

1 回答 1

3

编辑:看起来你的问题的真正根源是你只在getAllPlacesSELECT table_places.place_name中选择一列( )。试试吧。SELECT *

此外,这可能对将来有所帮助。而不是像这样对列索引进行硬编码:

...
p.setPlace_id(cursor.getInt(1));
...

尝试使用这样的东西:

...
p.setPlace_id(cursor.getInt(cursor.getColumnIndex(place_id)));
p.setPlace_name(cursor.getString(cursor.getColumnIndex(place_name)));
...
于 2013-10-10T17:32:59.783 回答