从ListView单击启动新活动

时间:2014-12-13 01:02:40

标签: android database sqlite listview onitemclick

我一直在试图弄清楚如何创建一个代码,允许用户 输入数据,后来存储在 SQLite数据库。然后,一些数据显示在 ListView 中。接下来,当有人点击列表项时,用户会转到新的活动,其中会显示与数据相对应的其余数据显示在单击用户 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;
}

}

这是我尝试获取特定数据的活动

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;
}  

}

1 个答案:

答案 0 :(得分:0)

使用您想要的位置在列表视图上调用getItemAtPosition()。