通过上下文菜单删除后刷新自定义列表视图

Posted

技术标签:

【中文标题】通过上下文菜单删除后刷新自定义列表视图【英文标题】:refresh custom list view after deleting through context menu 【发布时间】:2015-05-16 09:53:24 【问题描述】:

大家好,我是 android 开发的新手。我需要你们的帮助, 我一直在寻找几个小时,但找不到足够好的解决方案。我创建了一个简单的警报应用程序,它有一个自定义列表视图来显示所有可用的警报。我添加了一个上下文菜单,长按可以让用户删除警报。我能够编写代码来删除警报,但我似乎无法在它删除后刷新列表视图(我尝试了 this.recreate 但那很糟糕)

我确定我必须使用 notifyDataSetChanged(),但是当我使用 myCustomAdapter.notifyDataSetChanged() 时它没有显示为一种方法,我已经寻找解决方案很多天了,但似乎无法找到

p>

还要注意我的数据库相关方法 updateAlarm() addAlarm() 和 deleteAlarm() 在 DBHandler 类中(我不确定它们是否应该存在或在 customAdapter 类中,在通过***中的一些其他问题线程之后)

//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);
    

【问题讨论】:

能否贴出自定义 ListView 类的代码? 是的,我刚刚用它更新了帖子,但我没有将它用于删除功能。 【参考方案1】:

您是否致电updateResult() 并在删除后传递您的 ArrayList?

使用

yourAdapter. updateResults( results);

【讨论】:

是的,但是当我通过适配器调用它时,它没有显示“updateResults”方法。我在 onCreate() 之前将我的适配器添加为全局变量,以便我的 onContext.... 可以访问它 有人可以帮帮我吗!我有 3 天的时间来完成这件事,我被困了好几天!!当我使用 myCustomAdaptor.notifyDataSetChanged();我的主要活动 .notify.... 部分显示为红色,表示它不是方法,我的 updateResults() 方法也没有出现?!我究竟做错了什么???上帝请谁能帮帮我!!!!

以上是关于通过上下文菜单删除后刷新自定义列表视图的主要内容,如果未能解决你的问题,请参考以下文章

Android 列表视图刷新

带有列表子项可聚焦android的列表视图上下文菜单

在列表视图的上下文菜单中实现复制和粘贴

如何获取所选列表视图项中的id以打开上下文菜单并通过下一个活动来实现目标。

如何自定义 PDFView 的上下文菜单?

从列表视图中删除一行后刷新片段