0

Here's my code-

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {

        builder1 = new AlertDialog.Builder(context);
        alert1 = builder1.create();
        layoutInflater1 = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        layout1 = layoutInflater1.inflate(R.layout.alert_one, null);
        alert1.setView(layout1);

        buttonAlert_oneDelete = (Button) layout1.findViewById(R.id.buttonAlert_oneDelete);
        buttonAlert_oneUpdate = (Button) layout1.findViewById(R.id.buttonAlert_oneUpdate);
        buttonAlert_oneCancel = (Button) layout1.findViewById(R.id.buttonAlert_oneCancel);

        buttonAlert_oneDelete.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
                funDelete(id);
                simpleCursorAdapter.notifyDataSetChanged();
                alert1.cancel();
            }
        });

        buttonAlert_oneCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                alert1.cancel();
            }
        });     
        alert1.show();
    }
});

funDelete method

private void funDelete(long id) {
  db = abcDbHelper.getReadableDatabase(); 
  db.delete(AbcDbContract.TableWords.TABLE_NAME,AbcDbContract.TableWords.COLUMN_NAME_ID + " = " + String.valueOf(id), null); 
}

Hi! A method notifyDataSetChanged() does not ListView at once after deleting the line from database. How this make? Thank you very much.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
Anton
  • 147
  • 9
  • can you explain your question a little more.. – Santanu Sur Feb 13 '18 at 08:27
  • show your function delete and initializing statement of adapter.. – Santanu Sur Feb 13 '18 at 08:28
  • I guess this is because deleting from the database is an async function. If you call `notifyDataSetChanged` after calling `funDelete`, the async function may not have completed by that time. Call `notifyDataSetChanged` inside `funDelete` where you can be sure that the delete operation has completed. – Ishita Sinha Feb 13 '18 at 08:34
  • Post your `funDelete(id)` method code. and where you get `id` ? – Abu Yousuf Feb 13 '18 at 08:35
  • When I press a button "DELETE" in this dialog window the ListView must changes now – Anton Feb 13 '18 at 08:35
  • `private void funDelete(long id) { db = abcDbHelper.getReadableDatabase(); db.delete(AbcDbContract.TableWords.TABLE_NAME, AbcDbContract.TableWords.COLUMN_NAME_ID + " = " + String.valueOf(id), null); }` – Anton Feb 13 '18 at 08:37
  • Just edit your question and add this code below – Abu Yousuf Feb 13 '18 at 08:38

1 Answers1

0

Adapter doesn't know about your Database Table. Adapter only know about its data source which is List or Array. So in this scenario you have to reload data from Database and update Adapter.

 buttonAlert_oneDelete.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    funDelete(id);
    // reload datafrom Database
    // update datasource of the  simpleCursorAdapter 
    simpleCursorAdapter.notifyDataSetChanged();
    alert1.cancel();
   }
});

If you use CursorLoader then you don't need to query manually. CursorLoader automatically registers a ContentObserver to trigger a reload when data changes. You just have to call getContext().getContentResolver().notifyChange(uri, null); after any update/insert/delete operation.

Check this answer how to implement it .

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • Abu Yousuf thank you very much! I make this problem. You can look [here](https://ru.stackoverflow.com/questions/784387/%d0%ba%d0%b0%d0%ba-%d1%81%d0%b4%d0%b5%d0%bb%d0%b0%d1%82%d1%8c-%d1%81%d0%b8%d0%bd%d1%85%d1%80%d0%be%d0%bd%d0%b8%d0%b7%d0%b0%d1%86%d0%b8%d1%8e-%d1%81-listview-%d0%b8%d0%b7-%d0%b4%d0%b8%d0%b0%d0%bb%d0%be%d0%b3%d0%be%d0%b2%d0%be%d0%b3%d0%be-%d0%be%d0%ba%d0%bd%d0%b0?noredirect=1#comment1232161_784387) – Anton Feb 13 '18 at 10:38
  • Don't know russian language :p – Abu Yousuf Feb 13 '18 at 10:41