嗨,大家好,我是Android开发的新手。我需要你的帮助, 我一直在寻找好几个小时但找不到足够好的解决方案。我创建了一个简单的警报应用程序,它具有自定义列表视图,可显示所有可用的警报。我添加了一个上下文菜单,长按一下,用户可以删除警报。我能够编写代码来删除警报,但我似乎无法在删除后刷新列表视图(我试过这个。再创建但很糟糕)
我确定我必须使用notifyDataSetChanged()但是当我使用myCustomAdapter.notifyDataSetChanged()时它没有显示为方法我现在一直在寻找解决方案很多天似乎不能找出
还要注意我的db相关方法updateAlarm()addAlarm()和deleteAlarm()都在DBHandler类中(我不确定它们是否应该存在或者在customAdapter类中,经过stackoverflow中的一些其他问题线程后)
//MainActivity.java
private ListAdapter myCustomAdaptor; //global
public boolean onContextItemSelected(MenuItem item) {
myDbHandler = new DBHandler(MainActivity.this, null, null, 1);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Alarm alarm = alarmArray.get(info.position);
int aid = alarm.getAlarmId();
if(item.getItemId()==R.id.delete) {
alarmArray = myDbHandler.deleteAlarm(aid);
Toast.makeText(MainActivity.this," Alarm Deleted Successfully" ,Toast.LENGTH_LONG).show();
//NEED A WAY TO REPLACE THIS, BUT MUCH SMOOTHER :D
MainActivity.this.recreate();
return true;
}if(item.getItemId()==R.id.edit){
Intent i = new Intent(this.getApplicationContext(),AddNewAlarm.class);
i.putExtra("alarmId",listId);
startActivity(i);
Toast.makeText(MainActivity.this," " ,Toast.LENGTH_LONG).show();
return true;
}else {
return super.onContextItemSelected(item);
}
}
这里是customAdapter.java类的代码(不用于实现删除)
公共类CustomAdapter扩展了ArrayAdapter {
Context myContext;
int myCustomLayoutId;
ArrayList<Alarm> alarmArray;
public CustomAdapter(Context context, int resource, ArrayList<Alarm> alarms) {
super(context, resource, alarms);
this.myContext = context;
this.myCustomLayoutId = resource;
this.alarmArray = alarms;
}
@Override
public Alarm getItem(int position) {
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
PlaceHolder holder = null;
//if we currently don't have anny row view to reuse..
if(row==null){
//inflate the layout for a single row
LayoutInflater myInflater = LayoutInflater.from(myContext);
row = myInflater.inflate(myCustomLayoutId,parent,false);
holder = new PlaceHolder();
//get a reference to the different view elements we wish to update
holder.listId =(TextView) row.findViewById(R.id.txtAlarmId);
holder.listHour =(TextView) row.findViewById(R.id.showHour);
holder.listMin =(TextView) row.findViewById(R.id.showMin);
holder.listDayM =(TextView) row.findViewById(R.id.showDayM);
holder.listDayT =(TextView) row.findViewById(R.id.showDayT);
holder.listDayW =(TextView) row.findViewById(R.id.showDayW);
holder.listDayTh =(TextView) row.findViewById(R.id.showDayTh);
holder.listDayF =(TextView) row.findViewById(R.id.showDayF);
holder.listDayS =(TextView) row.findViewById(R.id.showDayS);
holder.listDaySu =(TextView) row.findViewById(R.id.showDaySu);
holder.onOffAlarm =(ImageView) row.findViewById(R.id.imageButton);
row.setTag(holder);
}else{
holder = (PlaceHolder) row.getTag();
}
//get the data from the alarm array
Alarm alarm = alarmArray.get(position);
//Setup and reuse the same listener for each row
holder.listId.setText(convertToDoubleDigits(alarm.getAlarmId()));
holder.listHour.setText(convertToDoubleDigits(alarm.getHour()));
holder.listMin.setText(convertToDoubleDigits(alarm.getMinute()));
if(alarm.getDayM()){
holder.listDayM.setTextColor(0xff0098ff);
}
if(alarm.getDayT()){
holder.listDayT.setTextColor(0xff0098ff);
}
if(alarm.getDayW()){
holder.listDayW.setTextColor(0xff0098ff);
}
if(alarm.getDayTh()){
holder.listDayTh.setTextColor(0xff0098ff);
}
if(alarm.getDayF()){
holder.listDayF.setTextColor(0xff0098ff);
}
if(alarm.getDayS()){
holder.listDayS.setTextColor(0xff0098ff);
}
if(alarm.getDaySu()){
holder.listDaySu.setTextColor(0xff0098ff);
}
if(alarm.getStatus()){
holder.onOffAlarm.setImageResource(R.drawable.ic_alarm_on);
}else{
holder.onOffAlarm.setImageResource(R.drawable.ic_alarm_off);
}
//set on click listener on ImageView
holder.onOffAlarm.setOnClickListener(ToggleListener);
Integer rowPosition = position;
holder.onOffAlarm.setTag(rowPosition);
return row;
}
View.OnClickListener ToggleListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer viewPosition = (Integer) v.getTag();
Alarm alarm = alarmArray.get(viewPosition);
DBHandler myDbHandler = new DBHandler(v.getContext(),null,null,1);
updateResults(myDbHandler.toggleAlarm(alarm.getAlarmId()));
}
};
public void updateResults(ArrayList<Alarm> results) {
alarmArray = results;
//Triggers the list update
notifyDataSetChanged();
}
//Place holder to hold view references
private static class PlaceHolder{
TextView listId;
TextView listHour;
TextView listMin ;
TextView listDayM ;
TextView listDayT;
TextView listDayW ;
TextView listDayTh ;
TextView listDayF ;
TextView listDayS ;
TextView listDaySu;
ImageView onOffAlarm ;
}
//Method to convert 12 hour time to 24 hort 1:1 to 01:01
private String convertToDoubleDigits(int i){
if(i<10){
return "0" +String.valueOf(i);
}else{
return String.valueOf(i);
}
}
答案 0 :(得分:0)
您是否在删除后调用了updateResult()
并传递了ArrayList?
使用
yourAdapter. updateResults( results);