Part4: update and delete the data in table
Update Screenshots:
Delete Screenshots:
Delete Screenshots:
Step 1: Add the
following update and delete methods in DBAdapter.java to manipulate the
database
DBAdapter.java
public Cursor getValuesById(int id){
return database.rawQuery("select * from
sample_table where _id = "+id, null);
}
public int updateValues(int id, String name,
String contact) {
ContentValues
values2 = new ContentValues();
values2.put("name", name);
values2.put("phone", contact);
return database.update(DBOpenHelper.SAMPLE_TABLE_NAME, values2,
"_id=?", new String[] { "" + id });
}
public int deleteById(int id){
return database.delete(DBOpenHelper.SAMPLE_TABLE_NAME, "_id=?", new String[]{""+id});
}
Step 2: Create context menu xml file to display option when long
click on the list item
res/menu/listview_context_menu.xml
listview_context_menu.xml
<?xml version="1.0"
encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/context_item_edit"
android:title="Edit"/>
<item
android:id="@+id/context_item_delete"
android:title="Delete"/>
</menu>
ViewAllDetails.java
Step 3: Register context menu for listView, write the following code
in onCreate()
registerForContextMenu(listView);
Step 4: Add the following code in onCreate()
Step 4: Add the following code in onCreate()
listView.setOnItemLongClickListener(new
OnItemLongClickListener() {
@Override
public boolean
onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String
id = ((TextView) arg1
.findViewById(R.id.custom_textView_id)).getText()
.toString();
selected_item_id = Integer.parseInt(id);
return false;
}
});
Step 5: Add two methods onCreateContextMenu and
onContextItemSelected in ViewAllDetails.java
@Override
public void
onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.listview_context_menu, menu);
super.onCreateContextMenu(menu,
v, menuInfo);
}
@Override
public boolean
onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_item_edit:
dialog = new
Dialog(ViewAllActivity.this);
dialog.setTitle("Edit
Details");
dialog.setContentView(R.layout.custom_dialog);
dialog.show();
Cursor
cursor = dbAdapter.getValuesById(selected_item_id);
cursor.moveToFirst();
editText_name = (EditText) dialog
.findViewById(R.id.custom_dialog_editText_name);
editText_phone = (EditText) dialog
.findViewById(R.id.custom_dialog_editText_phone);
Button
button_save = (Button) dialog
.findViewById(R.id.custom_dialog_button_save);
Button
button_cancel = (Button) dialog
.findViewById(R.id.custom_dialog_button_cancel);
editText_name.setText(cursor.getString(1));
editText_phone.setText(cursor.getString(2));
button_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String
name = editText_name.getText().toString();
String
phone = editText_phone.getText().toString();
int response = dbAdapter.updateValues(selected_item_id,
name,
phone);
if (response == -1) {
Toast.makeText(getApplicationContext(), "Data Failed Update", Toast.LENGTH_LONG).show();
}
else {
refreshListView();
Toast.makeText(getApplicationContext(), "Data Updated successfully", Toast.LENGTH_LONG)
.show();
}
dialog.dismiss();
}
});
button_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
break;
case R.id.context_item_delete:
int response = dbAdapter.deleteById(selected_item_id);
if (response == -1) {
Toast.makeText(getApplicationContext(), "Data failed to delete
successfully", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Data deleted successfully", Toast.LENGTH_LONG).show();
refreshListView();
}
break;
}
return super.onContextItemSelected(item);
}
Step 6: Add refreshListView() to refresh the listView content after
Update and Delete Items
private void refreshListView() {
cursor = dbAdapter.getAllValues();
cursor.moveToFirst();
MyAdapter
adapter = new MyAdapter();
listView.setAdapter(adapter);
}
No comments:
Post a Comment