0

I'm showing data from a database in a list view and I want to delete one entry when the user do a longclick in one row and then selects "yes" in a Dialog. I have all the code and it compiles but it isn't deleting anything. Any suggestion? Thanks

That's the code of the listview:

public class Consult extends FragmentActivity { 
    private ListView list;
    private SQLiteDatabase db;
    private int id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.consult);

        list = (ListView)findViewById(R.id.list);

        this.getIntent();

        ApeironsSQLiteHelper apeironsdb = new ApeironsSQLiteHelper(this, "DBApeirons.DB", null, 1);

        db = apeironsdb.getWritableDatabase();

        String[] campos = new String[]{"_id", "name", "kind_place", "score"};

        Cursor c = db.query("Apeirons", campos, null, null, null, null, null);

        c.moveToFirst();

        String[] from = new String[]{"name", "kind_place", "score"};

        int [] to = new int[] {R.id.Titulo, R.id.SubTitulo};

        //@SuppressWarnings("deprecation")
        MyListAdapter myadapter = new MyListAdapter (this, 
                R.layout.entrys, c, from, to);

        list.setAdapter(myadapter);

        list.setLongClickable(true);
        list.setOnItemLongClickListener(new OnItemLongClickListener(){
            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    int index, long arg3) {
                saveID(index);
                //db.delete("Apeirons", "_id=" + String.valueOf(index), null);
                Log.d("ONCLICK", String.valueOf(index));
                Log.d("ONCLICK", String.valueOf(id));
                callDialog();
                return false;
            }
        });

    }
    public void callDialog(){
        Log.d("DIALOG", String.valueOf(id));
        FragmentManager fragmentmanager = getSupportFragmentManager();
        SimpleDialog dialog = new SimpleDialog();
        dialog.saveIndex(id);
        //SimpleDialog.newInstance(id);
        dialog.show(fragmentmanager, "tag");
        Log.d("erase", "salgo del callDialog");
    }
    public void saveID(int ID){
        id = ID;
    }

And that's the code of the Dialog:

public class SimpleDialog extends DialogFragment {

    private SQLiteDatabase dbs;
    int ID;
    @Override
    public Dialog onCreateDialog (Bundle savedInstanceState){

        //ID = getArguments().getInt("id");

        ApeironsSQLiteHelper apeironsdbs = new ApeironsSQLiteHelper(getActivity(), 
                "DBApeirons.DB", null, 1);
        dbs = apeironsdbs.getWritableDatabase();

        AlertDialog.Builder builder = 
                new AlertDialog.Builder(getActivity());


        builder.setMessage(R.string.EraseDialogMessage);
        builder.setTitle(R.string.app_name);
        builder.setPositiveButton(R.string.EraseDialogPButton, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String args = String.valueOf(ID);
                Log.d("Yes BUTTON", args);
                /*String sql = "DELETE FROM Apeirons WHERE _id=" + args;
                dbs.execSQL(sql);*/
                //int prueba = dbs.delete("Apeirons", " _id = ?" + args, null);
                int prueba = dbs.delete("Apeirons", "_id = ?", new String[] { "" + args });
                Log.d("RETORNO DELETE", String.valueOf(prueba));
            }
        });

        builder.setNegativeButton(R.string.EraseDialogNButton, new DialogInterface.OnClickListener() {  
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();

            }
        });
        return builder.create();
    }

    public void saveIndex(int index){
        ID = index;
    }


}

I fixed!!! The problem was that I was using the id of the listview and it isn't the same in the database. Now I recover first the _id of the database and works perfect. Thank you for all the answers. The code to recover the _id of database below (maybe is useful to someone):

list.setLongClickable(true);
        list.setOnItemLongClickListener(new OnItemLongClickListener(){
            public boolean onItemLongClick(AdapterView<?> arg0, View v,
                    int index, long arg3) {
                c.moveToPosition(index);
                int id = c.getInt(c.getColumnIndex("_id"));
                saveID(id);
                Log.d("ONCLICK", String.valueOf(index));
                Log.d("ONCLICK", String.valueOf(id));
                callDialog();
                return false;
            }
        });
0

2 Answers 2

2

You can try

private SQLiteDatabase dbs;
String args = String.valueOf(ID);

Delete query:

Method 1:

dbs = apeironsdbs.getWritableDatabase();
String deleteQuery = "DELETE FROM Apeirons where _id='"+ args +"'";       
dbs .execSQL(deleteQuery);

Or you can use

Method 2:

ApeironsSQLiteHelper apeironsdb = new ApeironsSQLiteHelper(this, "DBApeirons.DB", null, 1);    
apeironsdb .delete(Apeirons, _id+"="+ID, null);   // ID is int value
Sign up to request clarification or add additional context in comments.

2 Comments

Really thanks you @Ritesh Gune I tried both but it´s still not working, I can´t understand why. The log cat doesn´t say anything interesting, just the messages that I print and some messages from the choreographer. Do you have any idea more?
I think the error is that I used the id of the cursor and I need the _id of the database and sometimes they aren't the same. Do you know how I can recover that _id?
0

Try change this line:

int prueba = dbs.delete("Apeirons", " _id = ?" + args, null);

into

int prueba = dbs.delete("Apeirons", " _id = \"" + args + "\"", null);

1 Comment

Really thank you, I tried but it's not working. I think the error is that I used the id of the cursor and I need the _id of the database and sometimes they aren't the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.