2016-03-25 146 views
0

我試圖刪除中的列表項目OnItemClick方法。數據庫是爲此目的而調用的,並且項目正在被刪除。但在此之後,我的列表視圖不會刷新,除非我再次調用該活動。此外,adapter.notifyDataSetChanged不起作用。請幫助。我花了幾個小時嘗試不同的解決方案,但都沒有工作。謝謝!Adapter.notifyDataSetChanged()不刷新列表視圖

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); 
     } 
    } 
} 
+1

更改你的'tasksArrayList = DBHandler.getInstance(c)中.getBothReminders();''由tasksArrayList.addAll(DBHandler.getInstance(c)中。 getBothReminders());' –

+0

在此行之前使用此行 DBHandler.getInstance(c).deleteReminder(tempReminder); upcomingReminderListAdapter.remove(tempReminder); –

+0

感謝噸@Raphael :)它的工作:) – Naila

回答

1

更改

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

通過

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

要更新tasksArrayList,但你不通過你的更新tasksArrayList到您的適配器。你的適配器有舊的值tasksArrayList和舊的值。這就是爲什麼你的觀點並沒有刷新即使notifyDataSetChanged()

0

您可以創建在適配器方法,該方法得到刷新列表的項目,然後設置notifyDataSetChanges():

例如在適配器中添加這個方法:

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

在你活動只需撥打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); 
        } 
... 

希望它有助於!