我目前正在开发一个小应用程序,它允许将人员详细信息存储在数据库中,然后将其检索到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 :(得分: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();