0

I have a listView who's populated by data inserted in my sqlite Database.

I created a contextMenu so I can be able to delete the Items from my ListView.

When I click on Delete, I want to delete the Item from my database and from my listView. But i'm not able and I don't know why. But I want to delete by getting the string of the Item I, because it does not work with a primary key.

Here's my code :

DatabaseHelper

 public void deleteCours(String cours)
{
    Open();
    db.execSQL("DELETE from "+TABLE_COURS+" WHERE "+COLONNE_COURS+"='"+cours+"'");
    db.close();
}

My ListView and the contextmenu

        //ce Listener permet de détecter si on clique sur un élément de la liste
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            //récupération de l'ID
            long itemid= lv.getItemIdAtPosition(i);
            //conversion en int
            int id= (int)itemid;
            //conversion en string du cours que je selectionne
            String a =lv.getItemAtPosition(i).toString();
            dbhelper.deleteCours(a);
            //Intent pour naviguer de pag
            /*Intent b = new Intent(AffichageCours.this,AffichageNotes.class);
            //création d0un Bundle pour transférer des données
            Bundle args = new Bundle();
            args.putString("nom_cours",a);
            args.putInt("id",id);
            //insertion du Bundle dans l'Intent
            b.putExtras(args);
            //démarrage de l'intent
            startActivity(b);*/
        }

    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu,menu);

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    switch(item.getItemId())
    {
        case R.id.supprimer:

            // I WANT TO DELETE HERE !
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

}

So, I need to get the string of the item so I can use it in my method. Ii want as well refresh my listview Automatically.

Thank's in advance ! Tell me if you need more code !

3
  • Please show full code Commented Apr 6, 2017 at 11:28
  • I edited my question Commented Apr 6, 2017 at 11:34
  • I edited my question with the full code as you asked ! Commented Apr 8, 2017 at 11:42

5 Answers 5

1

Rectify Query .

public void deleteCours(int getID) {
    Open();
    db.execSQL("DELETE from Table_name WHERE fieldName='"+getID+"'");
    db.close();
}

FYI

If you want to remove from Listview

adapter.remove(arrayList.get(position));  //Remove
adapter.notifyDataSetChanged();              //Refresh
Sign up to request clarification or add additional context in comments.

6 Comments

Well, now it deletes but it not deletes also in my database, because if I close and reopen the app, the Items I deleted appear again, so it's a problem with the delete..
@David now need to check why it happening .check your code.this logic is perfect
Do I need to change the code of the method that I use to get the data from the table ?
@David according to your code when ever app starts then for (int i = 0; arising .
@David please provide us full code .Main problem is solved .just need to rectify logic
|
0

Try this way

public void deleteCours(int id)
{
    Open();
    db.execSQL("DELETE FROM "+TABLE_COURS+" WHERE "+COLONNE_IDCOURS+"='"+id+"'");
    db.close();
}

Comments

0

The SQLiteDatabase should be getWritableDatabase().

Try this:

public void deleteCours(int id)
{
    Open();

    db.delete(TABLE_COURS, COLONNE_IDCOURS + " = ?", new String[]{String.valueOf(id)});
    db.close();
}

Comments

0

Use this to delete a specific row.

db.delete(TABLE_COURS, COLONNE_IDCOURS+ "=" + id, null);

3 Comments

Thank's ! How do i refresh the adapter now ? Because the content only disappears if I close and reopen the app..
if you are storing the data in and Collection(ArrayList) then either you should delete that data from ArrayList also or fetch all the data from database again. To update adapter we generally use adapterObject.notifysetdatachanged();
I tried but I cant use the adapter in the Context methods, it's not recognizing the adapter
0
public void deleteCours(int id) {
    Open();
    String Query = "DELETE * FROM "+TABLE_COURS+" WHERE "+COLONNE_IDCOURS+"="+id;
    Cursor cursor = db.rawQuery(Query, null);

    // db.close();
    if (cursor.getCount() < 1) {
        Toast.makeText(context,"Delete Successfull",Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(context,"Not Possible",Toast.LENGTH_SHORT).show();
    }
}

You can check really your data is being delete or not..

Or if not delete data then use some non-primary key field..

7 Comments

My data is not being deleted from the database.. How can I do it ?
id is primary key?
id is the id I get of my listview
What do you mean with "use non primary key fields "?
i think id is primary key..you can try with other non primary key column name
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.