2015-01-01 37 views
0

我對Android很陌生,我想這是一個愚蠢的問題,但我很樂意接受幫助。我在一個將數據庫設置爲SQLite的活動中有一段代碼。在另一個活動中,我想引用這個SQLite代碼,以便將其輸入到json中並將其發送到遠程服務器。Android - 使用其他活動的變量

問題是,它不能識別其他活動的變量。這裏是從數據庫創建數據到一個字符串的代碼。 在這個例子中,我想從db創建一個ArrayList,但它找不到我開發的set函數或表名。我錯過了什麼嗎?這裏是ArrayList中的代碼:

GpsPage.java

public class PersonsDatabaseHandler extends SQLiteOpenHelper { 

     //Database Name 
     private static final String DATABASE_NAME = "RecordsDB"; 

     //Table names 
     private static final TABLE_RECORD = "record"; 

     //Get all Persons 
     public ArrayList<Record> getAllPersons() { 
      ArrayList<Record> localList = new ArrayList<Record>(); 

      String selectQuery = "SELECT * FROM " + TABLE_RECORD; 

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

      //Loops through all rows and adds them to the local list 
      if(cursor.moveToFirst()) 
      { 
       do { 
        //Get person information 
        Record record = new Record(); 
        Record.setpLong(cursor.getString(0)); 
        Record.setpLat(cursor.getString(1)); 
        Record.setpAcc(cursor.getString(2)); 
        Record.setpTime(cursor.getString(3)); 

        //Add person to list 
        localList.add(record); 
       } while (cursor.moveToNext()); 
      } 

      return localList; 
     } 
    } 

這裏從兩個其他Java頁面(活動),第一個定義get和set的記錄代碼:

Record.Java

package com.program.android.taskir; 


public class Record { 

    //private variables 
    private int id; 
    private double pLong; 
    private double pLat; 
    private float pAcc; 
    private long pTime; 

    public Record(){} 
    // Empty constructor 

    // constructor 
    public Record(double pLong, double pLat, float pAcc, long pTime){ 
     super(); 
     this.pLong = pLong; 
     this.pLat= pLat; 
     this.pAcc= pAcc; 
     this.pTime= pTime; 
    } 

    @Override 
    public String toString() { 
     return "Record [id=" + id + ", Longtitude=" + pLong + ", Latitude=" + pLat + ", Accuracy" + pAcc + ", Time" +pTime 
       + "]"; 
    } 


    // getting ID 
    public int getID(){ 
     return this.id; 
    } 

    // setting id 
    public void setID(int id){ 
     this.id = id; 
    } 

    // getting pLong 
    public double getpLong(){ 
     return this.pLong; 
    } 

    // setting pLong 
    public void setpLong(double pLong){ 
     this.pLong = pLong; 
    } 

    // getting pLat 
    public double getpLat(){ 
     return this.pLat; 
    } 

    // setting pLat 
    public void setpLat(double pLat){ 
     this.pLat = pLat; 
    } 

    // getting pAcc 
    public float getpAcc(){ 
     return this.pAcc; 
    } 

    // setting pAcc 
    public void setpAcc(float pAcc){ 
     this.pAcc = pAcc; 
    } 

    // getting pTime 
    public long getpTime(){ 
     return this.pTime; 
    } 

    // setting pTime 
    public void setpTime(long pTime){ 
     this.pTime = pTime; 
    } 
} 

和其活性創建分貝:

MySQLiteHelper.java

package com.program.android.taskir; 

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

import java.util.LinkedList; 
import java.util.List; 

public class MySQLiteHelper extends SQLiteOpenHelper { 


    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "RecordsDB"; 

    public MySQLiteHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // SQL statement to create record table 
     String CREATE_RECORD_TABLE = "CREATE TABLE RECORD (" + 
       "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "latitude TEXT NOT NULL, " + 
       "longtitude TEXT NOT NULL, " + 
       "accuracy TEXT NOT NULL, " + 
       "time TEXT NOT NULL)"; 

     // create books table 
     db.execSQL(CREATE_RECORD_TABLE); 
    } 


    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older books table if existed 
     db.execSQL("DROP TABLE IF EXISTS Records"); 

     // create fresh record table 
     this.onCreate(db); 
    } 

    // Books table name 
    private static final String TABLE_RECORD = "record"; 

    // Books Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_LONG = "longtitude"; 
    private static final String KEY_LAT = "latitude"; 
    private static final String KEY_ACC = "accuracy"; 
    private static final String KEY_TIME = "time"; 

    private static final String[] COLUMNS = {KEY_ID, KEY_LONG, KEY_LAT, KEY_ACC, KEY_TIME}; 

    public void addRecord(Record record) { 
     //for logging 
     Log.d("addBook", record.toString()); 

     // 1. get reference to writable DB 
     SQLiteDatabase db = this.getWritableDatabase(); 

     // 2. create ContentValues to add key "column"/value 
     ContentValues values = new ContentValues(); 
     values.put(KEY_LONG, record.getpLong()); 
     values.put(KEY_LAT, record.getpLat()); 
     values.put(KEY_ACC, record.getpAcc()); 
     values.put(KEY_TIME, record.getpTime()); 

     // 3. insert 
     db.insert(TABLE_RECORD, // table 
       null, //nullColumnHack 
       values); // key/value -> keys = column names/ values = column values 

     // 4. close 
     db.close(); 
    } 

    public Record getRecord(int id) { 

     // 1. get reference to readable DB 
     SQLiteDatabase db = this.getReadableDatabase(); 

     // 2. build query 
     Cursor cursor = 
       db.query(TABLE_RECORD, // a. table 
         COLUMNS, // b. column names 
         " id = ?", // c. selections 
         new String[]{String.valueOf(id)}, // d. selections args 
         null, // e. group by 
         null, // f. having 
         null, // g. order by 
         null); // h. limit 

     // 3. if we got results get the first one 
     if (cursor != null) 
      cursor.moveToFirst(); 

     // 4. build book object 
     Record record = new Record(); 
     record.setID(Integer.parseInt(cursor.getString(0))); 
     record.setpLat(cursor.getDouble(1)); 
     record.setpLong(cursor.getDouble(2)); 
     record.setpAcc(cursor.getFloat(2)); 
     record.setpTime(cursor.getLong(2)); 

     //log 
     Log.d("getBook(" + id + ")", record.toString()); 

     // 5. return book 
     return record; 
    } 

    public List<Record> getAllRecords() { 
     List<Record> records = new LinkedList<Record>(); 

     // 1. build the query 
     String query = "SELECT * FROM " + TABLE_RECORD; 

     // 2. get reference to writable DB 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 

     // 3. go over each row, build book and add it to list 
     Record record = null; 
     if (cursor.moveToFirst()) { 
      do { 
       record = new Record(); 
       record.setID(Integer.parseInt(cursor.getString(0))); 
       record.setpLat(cursor.getDouble(1)); 
       record.setpLong(cursor.getDouble(2)); 
       record.setpAcc(cursor.getFloat(2)); 
       record.setpTime(cursor.getLong(2)); 


       // Add book to books 
       records.add(record); 
      } while (cursor.moveToNext()); 
     } 

     Log.d("getAllRecords()", record.toString()); 

     // return books 
     return records; 
    } 

    public int UpdateRecords(Record record) { 

     // 1. get reference to writable DB 
     SQLiteDatabase db = this.getWritableDatabase(); 

     // 2. create ContentValues to add key "column"/value 
     ContentValues values = new ContentValues(); 
     values.put("Latitude", record.getpLat()); // 
     values.put("Longtitude", record.getpLong()); 
     values.put("Accuracy", record.getpAcc()); 
     values.put("Time", record.getpTime()); 

     // 3. updating row 
     int i = db.update(TABLE_RECORD, //table 
       values, // column/value 
       KEY_ID + " = ?", // selections 
       new String[]{String.valueOf(record.getID())}); //selection args 

     // 4. close 
     db.close(); 

     return i; 

    } 

    public void deleteRecords(Record record) { 

     // 1. get reference to writable DB 
     SQLiteDatabase db = this.getWritableDatabase(); 

     // 2. delete 
     db.delete(TABLE_RECORD, //table name 
       KEY_ID + " = ?", // selections 
       new String[]{String.valueOf(record.getID())}); //selections args 

     // 3. close 
     db.close(); 

     //log 
     Log.d("deleteBook", record.toString()); 

    } 






} 

謝謝!

回答

1

你在找什麼是一個捆綁。它用於在活動之間傳遞數據。看看What is a "bundle" in an Android application,你可以理解它是如何完成的。

+0

謝謝。我試了一下,但摸索了一下。你能給我一個TABLE_RECORD的例子嗎? 謝謝 – GeoRay

相關問題