如何从列表视图中选择项目并将值传递到另一个页面以进行编辑或删除

时间:2018-02-07 17:36:00

标签: android listview android-sqlite

下面是我编写的草稿代码,用于选择列表视图中的项目并将其传递到另一个页面进行编辑。好的,现在在列表视图中,1项包括ID,身体温度,心率和BMI。现在,当我选择它们时,它们应该通过意图将值传递到另一个页面,我可以删除整个项目,因此它将完全从数据库中删除或者我可以编辑体温,心率和BMI。 ID我可以把它留作textview。但是现在我甚至无法选择它的项目来将值传递给另一个页面。请指教。

/*Codes for selecting and passing values to edit page*/
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             String BTemp = adapterView.getItemAtPosition(i).toString();
             String HRate = adapterView.getItemAtPosition(i).toString();
             String Bmi = adapterView.getItemAtPosition(i).toString();

             Cursor data = dbhelper.getRecID(BTemp, HRate, Bmi);
             int recID = -1;
             while(data.moveToNext()){
                 recID = data.getInt(0);
             }
             if(recID > -1){

                 Intent editScreenIntent = new Intent(ViewRecords.this, EditDel.class);
                 editScreenIntent.putExtra("id",recID);
                 editScreenIntent.putExtra("Body Temperature",BTemp);
                 editScreenIntent.putExtra("Heart Rate",HRate);
                 editScreenIntent.putExtra("BMI",Bmi);
                 startActivity(editScreenIntent);
             }
             else{
                 Toast.makeText(getApplicationContext(), "No ID Associated", Toast.LENGTH_LONG).show();
             }



 /* DatabaseHelper.java*/
     public Cursor getRecID(String BTemp, String Hrate, String Bmi){
            SQLiteDatabase db = this.getWritableDatabase();
            String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
                    " WHERE " + COL2 + " = '" + BTemp + "' AND " + COL3 + " = '" + Hrate + "' AND " + COL4 + " = '" + Bmi +"'";
            Cursor data = db.rawQuery(query, null);
            return data;
        }

0 个答案:

没有答案