Android Adapter.notifyDataSetChanged()未刷新列表视图
我正在尝试删除onimclick方法中的列表项。为此目的调用了DB,项目将被删除。但在此之后,我的列表视图不会刷新,除非我再次调用该活动。另外,adapter.notifyDataSetChanged不工作。请帮忙。我花了几个小时尝试不同的解决方案,但没有一个有效。谢谢Android Adapter.notifyDataSetChanged()未刷新列表视图,android,listview,adapter,notifydatasetchanged,Android,Listview,Adapter,Notifydatasetchanged,我正在尝试删除onimclick方法中的列表项。为此目的调用了DB,项目将被删除。但在此之后,我的列表视图不会刷新,除非我再次调用该活动。另外,adapter.notifyDataSetChanged不工作。请帮忙。我花了几个小时尝试不同的解决方案,但没有一个有效。谢谢 package com.fyp.digitalsecretary; import android.annotation.SuppressLint; import android.app.Activity; import andr
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);
}
}
}
package com.fyp.digitalsecretary;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Fragment;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.graphics.Color;
导入android.graphics.drawable.ColorDrawable;
导入java.util.ArrayList;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AbsListView;
//导入android.view.view.OnClickListener;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.DatePicker;
导入android.widget.EditText;
导入android.widget.TimePicker;
导入android.widget.Toast;
//导入android.widget.Button;
//导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.AdapterView.OnItemClickListener;
公共类UpComingMemberList扩展了主侦听器{
私有列表视图升级提醒列表视图;
私有数组列表任务数组列表;
私有动态适配器UpComingMemberListAdapter;
私人提醒临时提醒;
公共静态android.app.ActionBar ActionBar;
私有上下文c;
@SuppressLint(“新API”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.upcomming_任务);
Main.actionBar.setTitle(“即将到来的任务”);
tasksArrayList=DBHandler.getInstance(this.getBothReminders();
UpComingMemberListAdapter=新的DynamicListAdapter(getApplicationContext(),tasksArrayList);
UpComingMemberListView=(ListView)findViewById(R.id.lvTasks);
setAdapter(UpComingMemberListAdapter);
upcomingReminderListView.setOnItemClickListener(此);
UpComingMemberListAdapter.notifyDataSetChanged();
}
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
tempReminder=tasksArrayList.get(位置);
AlertDialog.Builder列表=新建AlertDialog.Builder(此);
最后一个字符串[]dept={“编辑”,“删除”};
list.setItems(dept,new DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
if(which==0){
编辑提醒();
}
否则{
DBHandler.getInstance(c).deleteRemembers(tempRemembers);
tasksArrayList.clear();
tasksArrayList=DBHandler.getInstance(c.getBothReminders();
UpComingMemberListAdapter.notifyDataSetChanged();
}
}
});
列表。设置标题(“请选择”);
list.create();
list.show();
}
公开提醒{
如果(tempReminder.isLoc==1){
意图编辑=新意图(这个,locbasedEminder.class);
edit.putExtra(“editReminder”,tempReminder);
这个。startActivity(编辑);
}
否则{
意图编辑=新意图(这个,timebasedMinder.class);
edit.putExtra(“editReminder”,tempReminder);
这个。startActivity(编辑);
}
}
}
您正在更新任务列表
,但未将更新后的任务列表
传递给适配器。您的适配器中有旧的tasksArrayList
,其中包含旧值。这就是为什么即使在notifyDataSetChanged()
您正在更新任务列表
但未将更新后的任务列表
传递给适配器后,视图也不会刷新的原因。您的适配器中有旧的tasksArrayList
,其中包含旧值。这就是为什么即使在notifyDataSetChanged()
之后视图也不会刷新的原因。您可以在适配器中创建方法,该方法使用项目刷新列表,然后设置notifyDataSetChanges():
例如,在适配器中添加以下方法:
public void notifyMyAdapter(ArrayList<ReminderItem> itemsList) {
this.theListInYourAdapter = itemsList;
notifyDataSetChanged();
}
希望有帮助 您可以在适配器中创建方法,该方法使用项目刷新列表,然后设置notifyDataSetChanges(): 例如,在适配器中添加以下方法:
public void notifyMyAdapter(ArrayList<ReminderItem> itemsList) {
this.theListInYourAdapter = itemsList;
notifyDataSetChanged();
}
希望有帮助 改变你的想法
tasksArrayList = DBHandler.getInstance(c).getBothReminders();
借
换衣服
tasksArrayList = DBHandler.getInstance(c).getBothReminders();
借
更改您的
tasksArrayList=DBHandler.getInstance(c.getBothReminders()代码>通过tasksArrayList.addAll(DBHandler.getInstance(c.getBothReminders())代码>在这一行之前使用这一行DBHandler.getInstance(c).deleteReminder(tempReminder);upcomingReminderListAdapter.remove(临时提醒);非常感谢@Raphael:)成功了:)@Khizar也感谢你的关心:)@RaphaelTeyssandier请将此作为答案发布,以便我可以投票……)更改您的tasksArrayList=DBHandler.getInstance(c.getBothReminders()代码>通过tasksArrayList.addAll(DBHandler.getInstance(c.getBothReminders())代码>在这一行之前使用这一行DBHandler.getInstance(c).deleteReminder(tempReminder);upcomingReminderListAdapter.remove(临时提醒);非常感谢@Raphael:)成功了:)@Khizar也感谢你的关心:)@RaphaelTeyssandier请将此作为答案发布,以便我可以投票……)