简体   繁体   中英

Cannot use notifyDataSetChanged to update adapter

I cannot update my ListView after deleting an item. Listview item gets deleted from database but I cannot update my adapter and it keeps showing deleted item in listView.

if I put adapter.notiftDataSetChanged(); it turns into red colored code strangely! And shows that cannot resolve the method notifyDataSetChanged().

Here is my code.

ArrayList<HashMap<String, String>> items = new ArrayList<>();
List<TRListFormat> list = trDb.getAllReminders();

for (TRListFormat val : list) {
    HashMap<String, String> map = new HashMap<>();
    map.put("title",val.getTitle());
    map.put("description", val.getDes());
    map.put("date", val.getDate());
    map.put("time", val.getTime());

    items.add(map);
}

adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
          new String[] { "title", "description", "date", "time" },
          new int[] {R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time });

lv = (ListView)findViewById(R.id.tbr_list);
lv.setAdapter(adapter);

my setOnItemClickListener , it displays AlertDialog with 3 buttons as the listview item is clicked.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> adapterView, View view, int position, final long id) {
            AlertDialog.Builder builder = new AlertDialog.Builder(TRList.this);
            builder.setTitle("Select the Action");
            builder.setMessage("Do anything");
            builder.setPositiveButton("Edit",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            int remId = (int)id;
                            Bundle dataBundle = new Bundle();
                            dataBundle.putInt("remId", remId);
                            Intent intent = new Intent(getApplicationContext(),TRTimeReminder.class);
                            intent.putExtras(dataBundle);
                            startActivity(intent);
                        }
                    });
            builder.setNeutralButton("Deactivate",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
            builder.setNegativeButton("Delete",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            int remId = (int)id + 1;
                            TRListFormat format = trDb.getReminder(remId);
                            String title = format.getTitle();
                            String des = format.getDes();
                            String date = format.getDate();
                            String time = format.getTime();
                            trDb.deleteReminder(new TRListFormat(remId, title, des, date, time));

                            Toast.makeText(getApplicationContext(), "Reminder: "+title+"deleted",
                                    Toast.LENGTH_SHORT).show();

                            adapter.notifyDataSetChanged();  //this line shows an error I have written above
                        }
                    });

            AlertDialog alert = builder.create();
            alert.show();
        }
    });

Solution 1 (Preferred)

notifyDataSetChanged is not available for SimpleAdapter . SimpleAdapter is meant for static data. You should use a different type of adapter, like ArrayAdapter . More information is here

Solution 2

Call this method instead of adapter.notifyDataSetChanged();

public void refreshList(){
    List<TRListFormat> list = trDb.getAllReminders();
    items = new ArrayList<>();
    for (TRListFormat val : list) {
        HashMap<String, String> map = new HashMap<>();
        map.put("title",val.getTitle());
        map.put("description", val.getDes());
        map.put("date", val.getDate());
        map.put("time", val.getTime());

        items.add(map);
    }

    adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
      new String[] { "title", "description", "date", "time" },
      new int[] {R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time     });

    lv.setAdapter(adapter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM