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. Then some of that data is displayed in a 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. 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. Does anyone know how I can find the data that is stored with the Row Id of the Item that was clicked on? 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 -
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;
}
}
call getItemAtPosition() on your list view, with the position you want.