Android 从列表视图开始新活动单击
我一直在试图找出如何创建一个代码,允许用户输入稍后存储在SQLite数据库中的数据。然后,其中一些数据将显示在列表视图中。接下来,当有人单击列表项时,它会将用户带到一个新活动,该活动显示与用户单击的ListView项中显示的内容相对应的其余数据。我已经设置了数据库,并且ListView中填充了我想要的数据,现在我需要弄清楚如何在用户单击列表项时启动的活动中显示正确的数据。有人知道我如何找到与单击的项目的行Id一起存储的数据吗?非常感谢您的帮助!另外,如果我需要更好地解释我的问题,请用任何问题进行评论 这是我的一些代码 数据库操作类-Android 从列表视图开始新活动单击,android,database,sqlite,listview,onitemclick,Android,Database,Sqlite,Listview,Onitemclick,我一直在试图找出如何创建一个代码,允许用户输入稍后存储在SQLite数据库中的数据。然后,其中一些数据将显示在列表视图中。接下来,当有人单击列表项时,它会将用户带到一个新活动,该活动显示与用户单击的ListView项中显示的内容相对应的其余数据。我已经设置了数据库,并且ListView中填充了我想要的数据,现在我需要弄清楚如何在用户单击列表项时启动的活动中显示正确的数据。有人知道我如何找到与单击的项目的行Id一起存储的数据吗?非常感谢您的帮助!另外,如果我需要更好地解释我的问题,请用任何问题进行
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 1;
public static final String ACC_NAME = "acc_name" ;
public static final String ACC_PASS = "acc_pass" ;
public static final String ACC_NOTES = "acc_notes" ;
// public static final String ACC_TYPE = "acc_type" ;
public static final String DATABASE_NAME = "acc_info" ;
public static final String TABLE_NAME = "table_info" ;
public static final String KEY_ROWID = "_id";
// Creating the Table
public String CREATE_QUERY = "CREATE TABLE table_info ( " +
"_id integer primary key autoincrement, "+
"acc_name TEXT, "+
"acc_pass TEXT, "+
"acc_notes TEXT )";
public DatabaseOperations(Context context) {
super(context, DBinfo.DATABASE_NAME, null, database_version);
// TODO Auto-generated constructor stub
Log.d("Database operations", "Database created");
}
@Override
public void onCreate(SQLiteDatabase sdb) {
// TODO Auto-generated method stub
sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
// Inserting the Information from edittexts in another activity
public void putInformation(DatabaseOperations dop, String acc_name, String acc_pass, String acc_notes)
{
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBinfo.ACC_NAME, acc_name);
cv.put(DBinfo.ACC_PASS, acc_pass);
cv.put(DBinfo.ACC_NOTES, acc_notes);
// cv.put(DBinfo.ACC_TYPE, type);
long k = SQ.insert(DBinfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One raw inserted");
}
// This populates the ListView
public Cursor getInformation(DatabaseOperations dop)
{
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] coloumns = {KEY_ROWID,DBinfo.ACC_NAME,DBinfo.ACC_PASS,DBinfo.ACC_NOTES};
Cursor CR = SQ.query(DBinfo.TABLE_NAME, coloumns, null, null, null, null, null);
return CR;
}
// This is really where I need help.. This code doesn't have errors, however it crashes upon
// clicking a ListView Item. This is also my attempt at a working code, it might not really
// make any sense and I am sorry about that. Again, I am trying to receive the info that
// corresponds with the row of info that was selected in the ListView.
public Cursor getName(DatabaseOperations dop) {
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] name102 = {KEY_ROWID,DBinfo.ACC_NAME};
Cursor c = SQ.query(DBinfo.TABLE_NAME, name102, null, null, null, null, null);
return c;
}
public Cursor getPass(DatabaseOperations dop) {
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] pass102 = {KEY_ROWID,DBinfo.ACC_PASS};
Cursor b = SQ.query(DBinfo.TABLE_NAME, pass102, null, null, null, null, null);
return b;
}
public Cursor getNotes(DatabaseOperations dop) {
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] notes102 = {KEY_ROWID,DBinfo.ACC_NOTES};
Cursor a = SQ.query(DBinfo.TABLE_NAME, notes102, null, null, null, null, null);
return a;
}
public String getNameRow(int prePosition) {
// TODO Auto-generated method stub
Cursor c = this.getName(this);
String NAME = c.getString(c.getColumnIndex(KEY_ROWID));
return NAME;
}
public String getPassRow(int prePosition) {
// TODO Auto-generated method stub
Cursor b = this.getPass(this);
String PASS = b.getString(b.getColumnIndex(KEY_ROWID));
return PASS;
}
public String getNotesRow(int prePosition) {
// TODO Auto-generated method stub
Cursor a = this.getNotes(this);
String NOTES = a.getString(a.getColumnIndex(KEY_ROWID));
return NOTES;
}
}
这是我试图获取特定数据的活动
通行证-
public class PassView extends Activity {
Main_Screen mainScreen;
DBHelper myDb;
DatabaseOperations myDb1;
TextView nameView1;
TextView passView;
TextView notesView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_passview);
myDb1 = new DatabaseOperations(this);
mainScreen = new Main_Screen();
nameView1 = (TextView) findViewById(R.id.nameView1);
passView = (TextView) findViewById(R.id.passView);
notesView = (TextView) findViewById(R.id.notesView);
int prePosition = getIntent().getIntExtra("position", 1);
openDB();
String name0 = myDb1.getNameRow(prePosition);
String pass0 = myDb1.getPassRow(prePosition);
String notes0 = myDb1.getNotesRow(prePosition);
closeDB();
nameView1.setText(name0);
passView.setText(pass0);
notesView.setText(notes0);
}
private void closeDB() {
// TODO Auto-generated method stub
myDb = new DBHelper();
myDb.close();
}
private void openDB() {
// TODO Auto-generated method stub
myDb = new DBHelper();
myDb.open();
}
public int prePosition() {
// TODO Auto-generated method stub
int prePosition = getIntent().getIntExtra("position", 1);
return prePosition;
}
}
在列表视图中调用getItemAtPosition,使用您想要的位置。我想要的位置是否只是ID号?取决于您使用的数据结构。。就像您将数据作为列表一样。。您可以使用listview位置从列表中提取代码。我花了一点时间处理代码,但我得到了它!谢谢你的帮助!