2016-04-19 48 views
1

我目前正在研究一個小應用程序,它允許將個人詳細信息存儲在數據庫中,然後檢索到EditText字段中。目前我能夠將人員姓名存儲到數據庫中。但是我無法檢索這些數據。從SQL Lite數據庫檢索數據到EditText中

任何人都可以給任何幫助將不勝感激!

謝謝!

DatabaseHelper.java:

package my.example.com.mymedicare; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 
public DatabaseHelper(Context context) { 
    super(context, "user.db", null, 4); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE Users (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , FullName VARCHAR, Email VARCHAR, Number INTEGER, Age INTEGER, Nurse VARCHAR)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS Users"); 

    // Create tables again 
    onCreate(db); 

} 
} 

SQLHelper.java:

package my.example.com.mymedicare; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 

import java.io.File; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.List; 

public class SQLHelper { 
Context mContext; 
DatabaseHelper dbHelper; 
public SQLHelper(Context mContext) { 
    dbHelper = new DatabaseHelper(mContext); 
    this.mContext = mContext; 
} 



public boolean insert(String FullName, int i){ 
    SQLiteDatabase db = dbHelper.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put("FullName", FullName); 
     db.close(); 
     return true; 
    } 
    } 

Register.java:

package my.example.com.mymedicare; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Register extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 
}; 


public void btnCreate(View view) { 

    SQLHelper sqlHelper = new SQLHelper(this); 

    EditText addName = (EditText) findViewById(R.id.nameText); 
    sqlHelper.insert(addName.getText().toString(), 0); 

    Toast.makeText(this, "Your Account Details Have Been Saved", Toast.LENGTH_SHORT).show(); 
    startActivity(new Intent(Register.this, MainMenu.class)); 
}} 

Modify.java:

package my.example.com.mymedicare; 

import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.EditText; 

public class Modify extends AppCompatActivity { 

static SQLiteDatabase db; 

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

    EditText getName=(EditText)findViewById(R.id.nameText); 
} 
} 
+0

你的代碼在哪裏做查詢? –

+0

如果這就是你的意思,我已經將DatabaseHelper.java添加到原始文章中了?我不太確定如何檢索FullName數據並將其顯示在「nameText」EditText字段中。 – ZenoX

+0

您可以使用幾種不同的命令來獲取數據。從這裏開始:https://www.google.com/?gws_rd=ssl#safe=active&q=android+sqlhelper+get+data –

回答

1

請執行此步驟。

將此方法放入SQLHelper.java文件中。

public ArrayList<String> fetch_Edittext() { 
    ArrayList<String> arr_ = new ArrayList<String>(); 
    String selectQuery = "Select * FROM " + TABLE_SCANITEMS + " ORDER BY id"; 

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

    if (cursor.moveToFirst()) { 
     do { 
      arr_.add(cursor.getString(0)); 

     } while (cursor.moveToNext()); 
    } 
    db.close(); 
    return arr_; 
} 

把這個類在你修改Java類

private class GetEdittext extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // dialog start 
     Log.e("Currency", "3"); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     ArrayList<String> arr_ = new ArrayList<String>(); 
     arr_ = db.fetch_Edittext(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void resul) { 
     super.onPostExecute(resul); 
     // dialog dismiss 
     //hear set your edit text 
    } 
} 

然後調用這個類在上創建類似的方法。

new GetEdittext().execute(); 
+0

感謝您的回覆。現在我得到的唯一錯誤是TABLE_SCANITEMS的「無法解析符號」和getWritableDatabase的「無法解析方法」 – ZenoX

+0

編輯:修復了getWritableDatabase錯誤。不知道如何修復「無法解析符號」TABLE_SCANITEMS – ZenoX

+0

@ZenoX首先卸載舊的apk文件,然後將此代碼設置爲DatabaseHelper.java類「db.execSQL(createtbl);」在oncreate方法。 –