我正在尝试删除 OnItemClick 方法中的列表项。为此目的调用DB,项目将被删除。但之后我的列表视图没有刷新,除非我再次调用该活动。此外,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);
}
}
}
答案 0 :(得分:2)
您正在更新<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="650"
android:fromAlpha="1.0"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:toAlpha="0.1" />
,但您没有将更新的tasksArrayList
传递给您的适配器。您的适配器具有旧tasksArrayList
,其中包含旧值。这就是为什么即使在tasksArrayList
答案 1 :(得分:1)
更改您的
tasksArrayList = DBHandler.getInstance(c).getBothReminders();
通过
tasksArrayList.addAll(DBHandler.getInstance(c).getBothReminders());
答案 2 :(得分: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);
}
...
希望它有所帮助!