Adapter.notifyDataSetChanged() 不刷新列表视图

Adapter.notifyDataSetChanged() not refreshing the list view

我试图在 OnItemClick 方法中删除列表项。为此目的调用 DB 并且项目被删除。但是在那之后我的列表视图不会刷新,除非并且直到我再次调用 activity 。此外,adapter.notifyDataSetChanged 不工作。请帮助。我花了几个小时尝试不同的解决方案,但 none 是 working.Thanks!

package com.fyp.digitalsecretary;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import java.util.ArrayList;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
//import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
//import android.widget.Button;
//import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


public class UpcomingReminderList extends Main implements OnItemClickListener {
    private ListView                    upcomingReminderListView;
    private ArrayList<ReminderItem>     tasksArrayList ;
    private DynamicListAdapter          upcomingReminderListAdapter;
    private ReminderItem                tempReminder;
    public static android.app.ActionBar actionBar ;
    private Context                     c;

    @SuppressLint("NewApi") 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView (R.layout.upcomming_tasks);      
        Main.actionBar.setTitle("Upcoming Tasks");              
        tasksArrayList = DBHandler.getInstance(this).getBothReminders();

        upcomingReminderListAdapter = new DynamicListAdapter(getApplicationContext(),tasksArrayList);
        upcomingReminderListView = (ListView) findViewById(R.id.lvTasks);

        upcomingReminderListView.setAdapter(upcomingReminderListAdapter);
        upcomingReminderListView.setOnItemClickListener(this);
        upcomingReminderListAdapter.notifyDataSetChanged();

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        tempReminder = tasksArrayList.get(position);
        AlertDialog.Builder list = new AlertDialog.Builder(this);

        final String[] dept = {"Edit", "Delete"};
        list.setItems(dept, new DialogInterface.OnClickListener() {
        @Override
            public void onClick(DialogInterface dialog, int which) {

                if(which == 0 ){
                    editReminder();
                }
                else {
                    DBHandler.getInstance(c).deleteReminder (tempReminder);
                    tasksArrayList.clear();
                    tasksArrayList = DBHandler.getInstance(c).getBothReminders();
                    upcomingReminderListAdapter.notifyDataSetChanged();
                }

            }
        });
        list.setTitle("Please Select");
        list.create();
        list.show();        
    }

    public void editReminder(){
        if (tempReminder.isLoc == 1) {
            Intent edit = new Intent(this, LocBasedReminder.class);
            edit.putExtra ("editReminder" , tempReminder);                  
            this.startActivity(edit);
        }
        else {
            Intent edit = new Intent(this, TimeBasedReminder.class);
            edit.putExtra ("editReminder" , tempReminder);                  
            this.startActivity(edit);
        }
    }
}

您正在更新您的 tasksArrayList,但您没有将更新后的 tasksArrayList 传递给您的适配器。你的适配器有旧的tasksArrayList,里面有旧的值。这就是为什么即使在 notifyDataSetChanged()

之后您的视图也没有刷新的原因

您可以在您的适配器中创建方法来刷新项目列表,然后设置 notifyDataSetChanges():

例如在你的适配器中添加这个方法:

public void notifyMyAdapter(ArrayList<ReminderItem> itemsList) {
        this.theListInYourAdapter = itemsList;
        notifyDataSetChanged();
    }

在你的 activity 中直接调用 upcomingReminderListAdapter.notifiMyAdapter(tasksArrayList); insead upcomingReminderListAdapter.notifyDataSetChanged();

...
                   if(which == 0 ){
                        editReminder();
                    }
                    else {
                        DBHandler.getInstance(c).deleteReminder (tempReminder);
                        tasksArrayList.clear();
                        tasksArrayList = DBHandler.getInstance(c).getBothReminders();
                        upcomingReminderListAdapter.notifiMyAdapter(tasksArrayList);
                    }
...

希望对您有所帮助!

改变你的

tasksArrayList = DBHandler.getInstance(c).getBothReminders(); 

来自

tasksArrayList.addAll(DBHandler.getInstance(c).getBothReminders());