在我的应用中,我将数据保存到 SQLite 并显示在 listview 中,但稍后我想基于 onitemClickListner listview进入 edittext 中的下一个活动。但我无法做到这一点。如何基于id生成用于获取数据的选择查询?
我的databasehelper类
public class DatabaseHelper extends SQLiteOpenHelper {
//Constants for Database name, table name, and column names
public static final String DB_NAME = "NamesDB";
public static final String TABLE_NAME = "names";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_STATUS = "status";
//Constructor
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
//creating the database
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME
+ "(" + COLUMN_ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME +
" VARCHAR, " + COLUMN_STATUS +
" TINYINT);";
db.execSQL(sql);
}
public boolean addName(String name, int status) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NAME, name);
contentValues.put(COLUMN_STATUS, status);
db.insert(TABLE_NAME, null, contentValues);
db.close();
return true;
}
这就是我将数据保存到sqlite的方式
private NameAdapter nameAdapter;
//saving the name to local storage
private void saveNameToLocalStorage(String name, int status) {
editTextName.setText("");
db.addName(name, status);
Name n = new Name(name, status);
names.add(n);
refreshList();
}
将数据加载到listview
private DatabaseHelper db;
private void loadNames() {
names.clear();
Cursor cursor = db.getNames();
if (cursor.moveToFirst()) {
do {
Name name = new Name(
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME)),
cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_STATUS))
);
names.add(name);
} while (cursor.moveToNext());
}
nameAdapter = new NameAdapter(this, R.layout.names, names);
listViewNames.setAdapter(nameAdapter);
}
它在listview中显示数据
我获取了listview中单击项的id并将其发送到我想运行查询的下一个活动,并获取特定ID的数据,但我无法执行此操作。
listViewNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int id, long l) {
Toast.makeText(MainActivity.this, id+"", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,Second.class);
intent.putExtra("id", id);
startActivity(intent);
}
});
答案 0 :(得分:0)
您需要从列表项中检索名称并查询数据库。添加列表项onclick。并使用position从适配器中检索名称和查询db之后。 db添加为intent的额外结果并打开新活动
答案 1 :(得分:0)
你的NameAdapter应该有这个方法:
public Name getItem(int position) {
return yourItems.get(position);
}
然后使用适配器按位置获取项目:
listViewNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Name yourObject = nameAdapter.getItem(position);
Toast.makeText(MainActivity.this, yourObject+"", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,Second.class);
intent.putExtra("yourObject", yourObject);
startActivity(intent);
}
});
如果你想把你的对象作为Extra传递,它应该实现Serializable或Parcelable。
<强>更新强>
选择方法:
public Cursor getNames(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT id, name, status FROM names where id = " + id, null);
return c;
}