Android的列表视图刷新 [英] Android List view refresh

查看:131
本文介绍了Android的列表视图刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView它是通过一个数组向我展示一些数据(这是另一个类,我通过它的对象访问它)。

每当从通过上下文菜单ListView中删除一个元素,该列表是不清爽,但元件从数组中删除。我怎样才能刷新列表以显示这个?

code:

 公共无效onCreateContextMenu(文本菜单菜单,视图V,
        ContextMenuInfo menuInfo){
    如果(v.getId()== R.id.mainListView){
        AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)menuInfo;
        交= info.position;
      menu.setHeaderTitle(股票[info.position]);
        的String []菜单项= STT;
        的for(int i = 0; I< menuItems.length;我++){
            menu.add(Menu.NONE,我,我,菜单项[I]);
            }
    }
  }

     @覆盖
      公共布尔onContextItemSelected(菜单项项){
        AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        INT menuItemIndex = item.getItemId();
        的String []菜单项= STT;
        字符串menuItemName =菜单项[menuItemIndex]
        listItemName =股[info.position]
        stockname.remove(后1);

    返回true;
  }
 

解决方案

您必须通知你的ListView适配器的数据已经改变。

  listViewAdapater.notifyDataSetChanged();
 

如果由于某些原因不能正常工作,有一些奇怪的情况下,现在看来似乎并没有通知,你可以通过用更新数组构造重新分配您的适配器。

I have one ListView which is showing me some data through an array (which is in another class and I'm accessing it through it's object).

Whenever I delete an element from the ListView through context menu, the list is not refreshing but the element is deleted from the array. How can I refresh the list to show this?

Code:

     public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.mainListView) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        post=info.position;
      menu.setHeaderTitle(stocks[info.position]);
        String[] menuItems = stt;
        for (int i = 0; i<menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
            }
    }
  }

     @Override
      public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String[] menuItems = stt;
        String menuItemName = menuItems[menuItemIndex];
        listItemName = stocks[info.position];
        stockname.remove(post-1);

    return true;
  }

解决方案

You have to notify your ListView adapter that the data has changed.

listViewAdapater.notifyDataSetChanged();

If that for some reason doesn't work and there are some wierd situations where it seems like it wasn't notifying, you can just reassign your adapter via the constructor with the updated array.

这篇关于Android的列表视图刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆