Issue
I'm trying to make a to do list using sqlite, where when I press on an item for long, it will get deleted from list(as well as from database). But I can't make it happen, can't understand how to delete(I'm a newbie). What can I do in setOnItemLongClickListener to delete value from database as well? This is my MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
private EditText e;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHelper helper = new DatabaseHelper(this);
lvItems = (ListView)findViewById(R.id.lvItems);
items = helper.getAllAcontacts();
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
e = (EditText)findViewById(R.id.etNewItem);
Button add = (Button)findViewById(R.id.btnAddItem);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!e.getText().toString().isEmpty()) {
if (helper.insert(e.getText().toString())){
Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Not Inserted", Toast.LENGTH_SHORT).show();
}
}
else{
e.setError("Enter To Do List Item");
}
items.clear();
items.addAll(helper.getAllAcontacts());
itemsAdapter.notifyDataSetChanged();
lvItems.invalidateViews();
lvItems.refreshDrawableState();
e.setText("");
}
});
setupListViewListener();
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(
new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter,
View item, int pos, long id) {
// Remove the item within array at position
items.remove(pos);
itemsAdapter.notifyDataSetChanged();
// Return true consumes the long click event (marks it handled)
return true;
}
});
}
And this is my database delete function:
public void deleteItem(long itemId) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DatabaseHelper.LIST_TABLE_NAME, "_id = ?",
new String[] { String.valueOf(itemId) });
}
Solution
I was able to solve it using cursor. The main problem was, my delete function could not get the id so used cursor to point to special position where I wanted to delete. Working code is:
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(
new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter,
View item, int pos, long id) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setTitle("DELETE OR UPDATE");
dialogBuilder.setMessage("Write in text edit box and the tap on UPDATE to update value or simply tap on DELETE to delete value.");
dialogBuilder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(!e.getText().toString().isEmpty()) {
if (helper.update(e.getText().toString(), pos)){
Toast.makeText(MainActivity.this, "Updated!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Not Updated", Toast.LENGTH_SHORT).show();
}
}
else{
e.setError("Enter To Do List Item");
}
items.clear();
items.addAll(helper.getAllAcontacts());
itemsAdapter.notifyDataSetChanged();
lvItems.invalidateViews();
lvItems.refreshDrawableState();
e.setText("");
}
});
dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
items.remove(pos);
helper.deleteItem(pos);
itemsAdapter.notifyDataSetChanged();
dialog.cancel();
}
});
dialogBuilder.create();
dialogBuilder.show();
return true;
}
});
}
And in DatabaseHelper:
public void deleteItem(int pos) {
SQLiteDatabase db = this.getWritableDatabase();
List<Integer> database_ids= new ArrayList<Integer>();
Cursor c = db.rawQuery("SELECT*FROM "+LIST_TABLE_NAME,null);
while(c.moveToNext()){
database_ids.add(Integer.parseInt(c.getString(0)));
}
db.delete(LIST_TABLE_NAME, " id=?",
new String[] { String.valueOf(database_ids.get(pos)) });
c.close();
}
Answered By - Mahin Rahman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.