在Main activity
我有一个listview
,用SimpleCursorAdapter
填充frome数据库。当我点击listview item
发送项目ID并打开包含edit activity
字段的edittext
时。
那么,我如何从数据库加载数据到edittext?
主要活动:
public class MainScreen extends FragmentActivity implements OnClickListener {
final String LOG_TAG = "myLogs";
ListView listViewMain;
ImageButton addButton;
CheckBox deleteCheck;
SQLiteDatabase db;
DataBase DB;
DBHelper dbHelper;
Cursor cursor;
SimpleCursorAdapter passListViewAdapter;
long itemId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
listViewMain = (ListView) findViewById(R.id.listViewMain);
listViewMain.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int itemSelected = position;
if (position == itemSelected){
Intent intentId = new Intent(MainScreen.this, ViewListItem.class);
intentId.putExtra("itemId", id);
Log.d(LOG_TAG, "add button : " + id);
startActivity(intentId);
}
}
});
}
@Override
protected void onResume() {
super.onResume();
loaderAdapter();
}
public void loaderAdapter(){
DB = new DataBase(this);
String[] from = new String[] { DataBase.COLUMN_TITLE, DataBase.COLUMN_DATE};
int[] to = new int[] { R.id.titleView, R.id.dateView };
passListViewAdapter = new SimpleCursorAdapter(
this,
R.layout.item,
DB.fetchAllPass(),
from,
to
, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listViewMain.setAdapter(passListViewAdapter);
}}
数据库活动:
public class DataBase {
public static final String DB_NAME = "appdb";
public static final int DB_VERSION = 1;
static final String TABLE_NAME = "passtab";
final String LOG_TAG = "myLogs";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title_enter";
public static final String COLUMN_LOGIN = "login_enter";
public static final String COLUMN_PASSWORD = "password_enter";
public static final String COLUMN_URL = "link_enter";
public static final String COLUMN_COMMENT = "comment_enter";
public static final String COLUMN_DATE = "date_enter";
public String results;
private DBHelper dbHelper;
private static Context mContext;
private SQLiteDatabase db;
private static final String DB_CREATE =
"create table " + TABLE_NAME + "(" +
COLUMN_ID + " integer primary key autoincrement, " +
COLUMN_TITLE + " VARCHAR(255), " +
COLUMN_LOGIN + " VARCHAR(255), " +
COLUMN_PASSWORD + " VARCHAR(255), " +
COLUMN_URL + " VARCHAR(255), " +
COLUMN_COMMENT + " text, " +
COLUMN_DATE + " VARCHAR(255)" +
");";
public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public DataBase(Context c) {
mContext = c;
}
public DataBase open() throws SQLiteException {
Log.d(LOG_TAG, "opening with cacheword");
dbHelper = new DBHelper(mContext);
db = dbHelper.getWritableDatabase();
System.gc();
return this;
}
public boolean isOpen (){
if (db !=null)
return db.isOpen();
else
return false;
}
public SQLiteDatabase getDatabase() {
return db;
}
public void close() {
if( dbHelper != null )
dbHelper.close();
}
public Cursor fetchPass(long rowId) throws SQLException {
openGuard();
Cursor mCursor = db.query(TABLE_NAME, new String[] {COLUMN_TITLE, COLUMN_LOGIN, COLUMN_PASSWORD, COLUMN_URL, COLUMN_DATE}, COLUMN_ID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
private void openGuard() throws SQLiteException {
if(isOpen()) return;
open();
if(isOpen()) return;
Log.d(LOG_TAG, "open guard failed");
throw new SQLiteException("Could not open database");
}}
第二项活动:
public class ViewListItem extends MainScreen implements OnClickListener{
final String LOG_TAG = "myLogs";
EditText comment_enter, link_enter, password_enter, login_enter, title_enter, date_enter;
Button delete_button, clear_close_button, save_changes_item_button;
CheckBox editCheck;
DBHelper db;
DataBase DB;
SimpleCursorAdapter passListViewAdapter;
SimpleDateFormat sdf;
private Cursor cursor = null;
long itemSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_item);
Intent intentId = getIntent();
long itemSelected = intentId.getLongExtra("itemId", 1);
Log.d(LOG_TAG, "received id : " + itemSelected);
cursor = DB.fetchPass(itemSelected);
String[] nnn = cursor;
comment_enter = (EditText) findViewById(R.id.comment_enter);
link_enter = (EditText) findViewById(R.id.link_enter);
password_enter = (EditText) findViewById(R.id.password_enter);
login_enter = (EditText) findViewById(R.id.login_enter);
title_enter = (EditText) findViewById(R.id.title_enter);
date_enter = (EditText) findViewById(R.id.date_enter);
}
@Override
protected void onPause() {
super.onPause();
finish();
Log.d(LOG_TAG, "onPause : ");
}
}
答案 0 :(得分:0)
我接下来解决这个问题: 在第二个活动中改变这一行:
cursor = DB.fetchPass(itemSelected);
String[] nnn = cursor;
为:
Cursor c = DB.fetchPass(itemSelected);
然后从光标中弹出每个字符串:
String title_str = c.getString(c.getColumnIndex("title_enter"));
String login_str = c.getString(c.getColumnIndex("login_enter"));
String pass_str = c.getString(c.getColumnIndex("password_enter"));
String link_str = c.getString(c.getColumnIndex("link_enter"));
String comment_str = c.getString(c.getColumnIndex("comment_enter"));
String date_str = c.getString(c.getColumnIndex("date_enter"));
收到的字符串设置为EditText字段。