所以我知道的textview是从数据库加载,但不是我想要的......
我会尽力解释这个:
数据库:
public class DBAdapter_Name {
public static final String KEY_ROWID = "_id";
public static final String KEY_ITEM = "name";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "NameDB";
private static final String DATABASE_TABLE = "charactername";
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_CREATE = "create table if not exists charactername (_id integer primary key autoincrement, "
+ "name VARCHAR not null);";
private final Context context;
private DatabaseHelper DBHelper_name;
private static SQLiteDatabase dbname;
public DBAdapter_Name(Context ctx) {
this.context = ctx;
DBHelper_name = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase dbname) {
try {
dbname.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase dbname, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
dbname.execSQL("DROP TABLE IF EXISTS charactername");
onCreate(dbname);
}
}
// ---opens the database---
public DBAdapter_Name open() throws SQLException {
dbname = DBHelper_name.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper_name.close();
}
// ---insert a record into the database---
public long insertRecord(String name) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ITEM, name);
return dbname.insert(DATABASE_TABLE, null, initialValues);
}
// ---deletes a particular record---
public boolean deleteContact(long rowId) {
return dbname.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
// ---retrieves all the records---
public static Cursor getAllRecords() {
return dbname.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM},
null, null, null, null, null, null);
}
// ---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException {
Cursor mCursor = dbname.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_ITEM }, KEY_ROWID + "=" + rowId, null, null,
null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public int deleteAll(){
return 0;
}
// ---updates a record---
public boolean updateRecord(long rowId, String name) {
ContentValues args = new ContentValues();
args.put(KEY_ITEM, name);
return dbname.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
我如何插入记录:
public class CreateNew extends Activity {
DBAdapter_Name dbname = new DBAdapter_Name(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new)
Button btnCreate = (Button) findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.editText1);
String nameTxt = editText1.getText().toString();
Intent PlayScreen = new Intent(getApplicationContext(),
PlayScreen.class);
// ------------------------------------------------------------------------------------------------------------------------------------
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
// ------------------------------------------------------------------------------------------------------------------------------------
dbname.open();
long insertedRowId = dbname.insertRecord(editText1.getText().toString()); //Get the rowId which was inserted.
dbname.close();
PlayScreen.putExtra("strAlliance", spinner1
.getSelectedItem().toString());
PlayScreen.putExtra("strRace", spinner2.getSelectedItem()
.toString());
PlayScreen.putExtra("strClass", spinner3.getSelectedItem()
.toString());
// ------------------------------------------------------------------------------------------------------------------------------------
startActivityForResult(PlayScreen, 0);
}
}
});
Button btnMoreInfo = (Button) findViewById(R.id.btnMoreInfo);
btnMoreInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent MoreInfo = new Intent(v.getContext(), MoreInfo.class);
startActivity(MoreInfo);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent BeforeYouBegin = new Intent(getApplicationContext(),
BeforeYouBegin.class);
startActivity(BeforeYouBegin);
finish();
}
editText显然是您输入想要名字的地方
我是如何显示记录的:
public void DisplayRecord(Cursor c) {
TextView textViewStrName = (TextView) findViewById(R.id.textViewStrName);
Cursor result = dbname.getRecord(insertedRowId);
int columnIndex = result.getColumnIndex(DBAdapter_Name.KEY_ITEM);
String insertedName = result.getString(columnIndex);
textViewStrName.setText(insertedName);
}
现在,无论您在查看输入内容时输入editText框的内容,您所看到的只是:name。
所以它正在阅读数据库,但不是我想要的......
答案 0 :(得分:0)
要显示您刚刚在数据库中输入的内容,您需要这样做。
//Inserting the value into DB.
dbname.open();
long insertedRowId = dbname.insertRecord(editText1.getText().toString()); //Get the rowId which was inserted.
dbname.close();
//Retrieving that value from DB & displaying it.
Cursor result = dbname.getRecord(insertedRowId);;
int columnIndex = result.getColumnIndex(DBAdapter_Name.KEY_ITEM);
String insertedName = result.getString(columnIndex);
textViewStrName.setText(insertedName);
答案 1 :(得分:0)
试试此代码
String name="";
dbname.open();
long rowId = dbname.insertRecord(editText1.getText().toString());
Cursor c = dbname.getRecord(rowId);
startManagingCursor(c);
if(c.moveToFirst()){
do{
name = c.getString(1);
}while (c.moveToNext());
}
dbname.close();
c.close();
textViewStrName.setText(name);
希望这有助于你。