In
this tutorial I will display inserted record and update and delete row. To display all records in list view you have to use Cursor Adapter class which allow you to set layout of your list item (Row) layout. When user click on list item it will redirect to update activity and
onItemLongclick method. it will ask for
delete the row from database and also update list view. In Database Demo : Database Operations with Cursor Adapter Class Part-1 i have already
done insert into the database. I hope you have seen that tutorial so
you can understand this tutorial. On update and delete you have to do two simultaneous
work update database and also update list view.
And Last Operation delete i have wrriten delete logic in List view OnItemLongClick method. so when user(Admin) Long press on List item. Alert dialog will popup to confirm delete operation. so now add this below code in DbHelper class for deleting row from Database after Update Method. delete method get id from list item and perform delete operation on the bases of it.
After update or delete you have to update view of list also. so to update you have to requery cursor object.like this
You can download code from here.
Keep Coding and cheers.....
Cursoradapter.java
package com.example.dbdemo; import android.content.Context; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.RatingBar; import android.widget.TextView; public class Cursoradapter extends CursorAdapter { public Cursoradapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } @Override public View newView(Context con, Cursor c, ViewGroup arg2) { // TODO Auto-generated method stub LayoutInflater inflater = (LayoutInflater) con.getSystemService(con.LAYOUT_INFLATER_SERVICE); View retView = inflater.inflate(R.layout.list_item, null); return retView; } @Override public void bindView(View v, Context con, Cursor c) { // TODO Auto-generated method stub TextView id = (TextView) v.findViewById(R.id.row_tv0); TextView pname = (TextView) v.findViewById(R.id.row_tv1); TextView des = (TextView) v.findViewById(R.id.row_tv2); RatingBar rb = (RatingBar) v.findViewById(R.id.row_rating); TextView qty = (TextView) v.findViewById(R.id.row_tv3); id.setText(c.getString(c.getColumnIndex(c.getColumnName(0)))); pname.setText(c.getString(c.getColumnIndex(c.getColumnName(1)))); des.setText(c.getString(c.getColumnIndex(c.getColumnName(2)))); rb.setRating(c.getFloat(c.getColumnIndex(c.getColumnName(3)))); rb.setIsIndicator(true); qty.setText(c.getString(c.getColumnIndex(c.getColumnName(4)))); } @Override protected void onContentChanged() { // TODO Auto-generated method stub super.onContentChanged(); notifyDataSetChanged(); } }
list_item.xml
display.xml
display.java
package com.example.dbdemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import android.widget.RatingBar; import android.widget.TextView; public class Display extends Activity implements OnItemClickListener { ListView lv; Cursoradapter adapt; Cursor c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); lv = (ListView) findViewById(R.id.listView1); final DbHelper helper = new DbHelper(this); helper.open(); new Handler().post(new Runnable() { @Override public void run() { c = helper.getAllData(); adapt = new Cursoradapter(Display.this, c); lv.setAdapter(adapt); } }); lv.setOnItemClickListener(this); lv.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView arg0, View v, int position, long arg3) { // TODO Auto-generated method stub TextView id = (TextView) v.findViewById(R.id.row_tv0); final int ids = Integer.parseInt(id.getText().toString()); AlertDialog.Builder ad = new AlertDialog.Builder(Display.this); //ad.setTitle("Notice"); ad.setMessage("Sure you want to delete item ?"); ad.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Delete of record from Database and List view. helper.delete(ids); c.requery(); adapt.notifyDataSetChanged(); lv.setAdapter(adapt); } }); ad.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); ad.show(); return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.display, menu); return true; } @Override public void onItemClick(AdapterView arg0, View v, int position, long arg3) { // TODO Auto-generated method stub TextView id = (TextView) v.findViewById(R.id.row_tv0); TextView pname = (TextView) v.findViewById(R.id.row_tv1); TextView des = (TextView) v.findViewById(R.id.row_tv2); RatingBar rb = (RatingBar) v.findViewById(R.id.row_rating); TextView qty = (TextView) v.findViewById(R.id.row_tv3); int ids = Integer.parseInt(id.getText().toString()); String pn = pname.getText().toString(); String d = des.getText().toString(); float r = rb.getRating(); int q = Integer.parseInt(qty.getText().toString()); Intent updat = new Intent(Display.this,Updatedata.class); updat.putExtra("id", ids); updat.putExtra("pname", pn); updat.putExtra("desc",d); updat.putExtra("rat",r); updat.putExtra("qty",q); startActivityForResult(updat,101); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case 101: c.requery(); adapt.notifyDataSetChanged(); lv.setAdapter(adapt); break; } } } }Now if user just click on list view item it will be redirect to the update activity. so check out onItemClick method code. it will send clicked item information to update activity so user won't require to enter all data again.
update_data.xml
Updatedata.java
package com.example.dbdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RatingBar; public class Updatedata extends Activity { Button update,cancel; EditText pname,desc,qty; RatingBar rb; DbHelper helper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.update_data); update = (Button) findViewById(R.id.update_save); cancel = (Button) findViewById(R.id.update_cancel); pname = (EditText) findViewById(R.id.update_et1); desc = (EditText) findViewById(R.id.update_et2); qty = (EditText) findViewById(R.id.update_et3); rb = (RatingBar) findViewById(R.id.update_rb); Intent dis = getIntent(); Bundle data = dis.getExtras(); final int id = data.getInt("id"); String pn = data.getString("pname"); String d = data.getString("desc"); float r = data.getFloat("rat", 0); int q = data.getInt("qty"); pname.setText(pn); desc.setText(d); rb.setRating(r); qty.setText(""+ q); helper = new DbHelper(Updatedata.this); helper.open(); update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String name = pname.getText().toString(); String des = desc.getText().toString(); int quan = Integer.parseInt(qty.getText().toString()); helper.update(id,name,des,quan); helper.close(); Intent display = new Intent(Updatedata.this,Display.class); setResult(RESULT_OK, display); finish(); } }); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub helper.close(); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.updatedata, menu); return true; } }Now Add this below code in DbHelper class before close method.
public void update(int id, String name, String des, int quan) { // TODO Auto-generated method stub ContentValues values = new ContentValues(); values.put(pname, name); values.put(desc, des); values.put(quantity, quan); ourdb.update(product_table, values, order_id +" = "+ id , null); }
And Last Operation delete i have wrriten delete logic in List view OnItemLongClick method. so when user(Admin) Long press on List item. Alert dialog will popup to confirm delete operation. so now add this below code in DbHelper class for deleting row from Database after Update Method. delete method get id from list item and perform delete operation on the bases of it.
public void delete(int ids) { // TODO Auto-generated method stub ourdb.delete(product_table, order_id + " = " + ids, null); }
After update or delete you have to update view of list also. so to update you have to requery cursor object.like this
c.requery(); adapt.notifyDataSetChanged();One more thing you have to take consider is when you use rating bar in list view list view onItemClick method won't work so if you are facing this you have to setthis property.
rb.setIsIndicator(true);So,that's lots of coding out require for all these operation. i know this is somehow confusing but when you do by yourself you will get this. if you have any query or problem then comment in below comment box.
You can download code from here.
Keep Coding and cheers.....
Good work
ReplyDelete