gpt4 book ai didi

java - Android 从数据库获取数据到编辑 TextView 时遇到问题

转载 作者:行者123 更新时间:2023-12-02 06:10:39 25 4
gpt4 key购买 nike

就像,如果我在编辑 TextView 中输入一些文本并按保存按钮,数据就会保存到数据库中。现在我希望,当我重新启动模拟器时,保存的文本将保留在编辑 TextView 中。我从数据库调用 getMsg() 方法来显示文本。请看看我的代码...

MainActivity.java

public class MainActivity extends Activity implements LocationListener {
String separator = "; ";
private Button btn_cntct;
private EditText message;
LinearLayout linearLayoutSec;
private Button GPSState;
Button b_alert;
Button b_save_message;
public int REQUESTCODE = 1;
int temp;
ScrollView ScrView;
Context c = MainActivity.this;
Button AlertMessages;
private static ArrayList<ContactItems> selectedContactList = new ArrayList<ContactItems>();
static CustomAdapter adapter;
public static String[] myvalue = new String[1];
Spinner spinner;
static String[] alert = { " ", "I am in danger", "Help Me", "Watch Out",
"Look For Me", "Cover ME" };
MySQLiteHelper dbHelper;
String savedMessage;
Button bsave;

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

message = (EditText) findViewById(R.id.et_message);
dbHelper = new MySQLiteHelper(this);
String dis = dbHelper.getMsg();
if (dis != null) {
message.setText(dis);
}

bsave = (Button) findViewById(R.id.b_save);
bsave.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = message.getText().toString();
dbHelper.updateMsg(str);

}
});

MySQLiteHelper.java

public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME = "userdetails";
public static final String TABLE_NAME_MSG = "usermessage";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NUMBER = "NUMBER";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_TYPE = "type";
public static final String COLUMN_ID_MSG = "_id";
public static final String COLUMN_MSG = "message";
private static final String DATABASE_NAME = "userInformation.db";

private static final int DATABASE_VERSION = 1;
private static final String LOG = "DatabaseHelper";

// Database creation sql statement
private static final String DATABASE_CREATE_TABLE_CONTACTS = "create table "
+ TABLE_NAME + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME
+ " text not null,"+COLUMN_NUMBER+" text not null,"+COLUMN_TYPE+" text not null);";
private static final String DATABASE_CREATE_TABLE_MSG = "create table "
+ TABLE_NAME_MSG + "(" + COLUMN_ID_MSG
+ " integer primary key autoincrement, " + COLUMN_MSG + " text not null);";
private static final String DATABASE_INSERT_MSG = "insert into "
+ TABLE_NAME_MSG + "(" + COLUMN_MSG+") VALUES('enter your message');";


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

@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE_TABLE_CONTACTS);
database.execSQL(DATABASE_CREATE_TABLE_MSG);
database.execSQL(DATABASE_INSERT_MSG);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public long insertContact(ContactItems contItem) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contItem.getName());
values.put(COLUMN_NUMBER, contItem.getNumber());
values.put(COLUMN_TYPE, contItem.getType());

// insert row
long result = db.insert(TABLE_NAME, null, values);

// assigning tags to todo
/*for (long tag_id : tag_ids) {
createTodoTag(todo_id, tag_id);
}*/

return result;
}


public ArrayList<ContactItems> getAllContacts() {
ArrayList<ContactItems> contactList = new ArrayList<ContactItems>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;

Log.e(LOG, selectQuery);

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

// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
ContactItems conItems = new ContactItems();
conItems.setName(c.getString((c.getColumnIndex(COLUMN_NAME))));
conItems.setNumber((c.getString(c.getColumnIndex(COLUMN_NUMBER))));
conItems.setType(Integer.parseInt(c.getString(c.getColumnIndex(COLUMN_TYPE))));

// adding to todo list
contactList.add(conItems);
} while (c.moveToNext());
}

return contactList;
}

public void updateMsg(String msg) {
// TODO Auto-generated method stub
// String selectQuery = "update " + TABLE_NAME_MSG+" set "+COLUMN_MSG+" = '"+msg+"' where "+COLUMN_ID_MSG+" = 1";
ContentValues values = new ContentValues();
values.put(COLUMN_MSG, msg);
SQLiteDatabase db = this.getReadableDatabase();
db.update(TABLE_NAME_MSG, values, null, null);

}
public String getMsg() {
String message ="";
String selectQuery = "SELECT * FROM " + TABLE_NAME_MSG;

SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
message = c.getString(c.getColumnIndex(COLUMN_MSG));
} while (c.moveToNext());
}

// TODO Auto-generated method stub
Log.e(LOG, "message = "+message);
return message;

}

public int removeData(String number) {
// TODO Auto-generated method stub

SQLiteDatabase db = this.getReadableDatabase();
//db.drawQuery(deleteQuery, null);
return db.delete(TABLE_NAME, COLUMN_NUMBER + "='" + number+"'", null) ;

}

}

最佳答案

我认为您的查询每次都会创建新表...

只需更改您的查询

private static final String DATABASE_CREATE_TABLE_CONTACTS = "create table if not exist "
+ TABLE_NAME + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME
+ " text not null,"+COLUMN_NUMBER+" text not null,"+COLUMN_TYPE+" text not null);";
private static final String DATABASE_CREATE_TABLE_MSG = "create table if not exist "
+ TABLE_NAME_MSG + "(" + COLUMN_ID_MSG
+ " integer primary key autoincrement, " + COLUMN_MSG + " text not null);";
<小时/>

我建议你必须使用SharedPreferences而不是数据库。

检查下面的代码..

public class MainActivity extends Activity implements LocationListener {
String separator = "; ";
private Button btn_cntct;
private EditText message;
LinearLayout linearLayoutSec;
private Button GPSState;
Button b_alert;
Button b_save_message;
public int REQUESTCODE = 1;
int temp;
ScrollView ScrView;
Context c = MainActivity.this;
Button AlertMessages;
private static ArrayList<ContactItems> selectedContactList = new ArrayList<ContactItems>();
static CustomAdapter adapter;
public static String[] myvalue = new String[1];
Spinner spinner;
static String[] alert = { " ", "I am in danger", "Help Me", "Watch Out",
"Look For Me", "Cover ME" };
MySQLiteHelper dbHelper;
String savedMessage;
Button bsave;

-------------------------------------
SharedPreferences sp; // edited
-------------------------------------

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

message = (EditText) findViewById(R.id.et_message);
-------------------------------------
sp=getSharedPreferences("user_data", Activity.MODE_PRIVATE); //Edited
String saved_value=sp.getString("share_key","");
message.setText(saved_value);
-------------------------------------
dbHelper = new MySQLiteHelper(this);
//String dis = dbHelper.getMsg();
//if (dis != null) {
// message.setText(dis);
// }

bsave = (Button) findViewById(R.id.b_save);
bsave.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = message.getText().toString();
//dbHelper.updateMsg(str);
-------------------------------------
sp.edit().putString("str", str1).commit(); /Edited
-------------------------------------
}
});

关于java - Android 从数据库获取数据到编辑 TextView 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21926962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com