[英]Starting a new Activity from a ListView click
I have been trying to figure out how to create a code that allows the user to input data that is later stored in an SQLite Database . 我一直在尝试找出如何创建允许用户 输入稍后存储在SQLite数据库中的数据的代码。 Then some of that data is displayed in a ListView .
然后,其中一些数据将显示在ListView中 。 Next, when someone clicks on a List Item , it takes the user to a new Activity that displays the rest of the data that corresponds with what was displayed in the ListView Item that the user clicked on.
接下来,当某人单击List Item时 ,它将带用户到一个新的Activity ,该活动显示与用户单击的ListView Item中显示的数据相对应的其余数据 。 I have the Database set up and the ListView is populated with the data I want there, now I need to figure out how to have the correct data displayed in the Activity that is started when the user clicks on a List Item .
我已经设置了数据库 ,并且用我想要的数据填充了ListView ,现在我需要弄清楚如何在用户 单击 List Item时启动的Activity中显示正确的数据。 Does anyone know how I can find the data that is stored with the Row Id of the Item that was clicked on?
有谁知道我如何找到与单击的项目的行ID一起存储的数据 ? All help is greatly appreciated!
非常感谢所有帮助! Also, please comment with any questions or if I need to explain my question better.
另外,如果有任何问题,或者如果我需要更好地解释我的问题,请发表评论。
Here is some of my code... 这是我的一些代码...
Database Operations class - 数据库操作类-
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;
}
}
This is the activity that I am trying to get the specific data to 这是我试图获取特定数据的活动
PassView - PassView-
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(),并指定所需的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.