2016-03-25 14 views
0

のリストアイテムを削除しようとしています。OnItemClickメソッド。 DBはこの目的のために呼び出され、アイテムは削除されています。しかし、その後、私のリストビューは、私がそのアクティビティを再び呼び出すまで、リフレッシュされません。また、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)によります '。 getBothReminder()); ' –

+0

この行の前にこの行を使用してください。 DBHandler.getInstance(c).deleteReminder(tempReminder); upcomingReminderListAdapter.remove(tempReminder); –

+0

ありがとうトン@ラファエル:)それは働いた:) – 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(); 
    } 

だけINSEAD upcomingReminderListAdapter.notifyDataSetChanged();

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

希望upcomingReminderListAdapter.notifiMyAdapter(tasksArrayList);呼び出します助けになる!

関連する問題