dimanche 3 avril 2016

How to update a specific row in database on a button click in dynamically generated table

Android Emulator

This is a dynamically generated table and on clicking the accept button only the corresponding row's 'status' field text 'pending' need to be updated to 'accepted'. What should be the code? My query udpates all rows status field from pending to accepted.

Adminactivity.java(dynamically generated column code)

 for (int j = 0; j < 1; j++)
 {
        TextView tv1 = new TextView(AdminActivity.this);
        tv1.setText("Acccept");
        tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                                    TableRow.LayoutParams.WRAP_CONTENT));
                                                    tv1.setGravity(Gravity.CENTER);
 tv1.setTextSize(12);
                           tv1.setPadding(5, 0, 0, 0);
                            // tv1.setTextColor(Color.BLACK);
                            tv1.isClickable();
                            tv1.setTextColor(Color.RED);
                            tv1.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                            row1.addView(tv1);


                       tv1.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {

                               // Integer iid=dbHelper.getid();
                               dbHelper.accept("accept", "pending",k);
                               Toast.makeText(getApplicationContext(), "Accepted", Toast.LENGTH_SHORT).show();
                               //   notify();
                           }
                       });
                    }

DBhelper.java public Cursor accept (String accept,String pending) {

    SQLiteDatabase db=this.getReadableDatabase();

           ContentValues contentValues = new ContentValues();

    String query="UPDATE register6 SET status='"+accept+"' WHERE status='"+pending+"'";

    Cursor cursor=db.rawQuery(query, null);
 if(cursor!=null) {

        cursor.moveToNext();
    }
        return cursor;
}

Aucun commentaire:

Enregistrer un commentaire