调用notifyDataSetChanged后,Android ListView不刷新

时间:2017-01-07 09:20:14

标签: android listview

在调用notifyDataSetChanged()方法后,我一直在listview上加载更多项目 以下是在listview上设置用户滚动操作的代码块

lv = (ListView) getActivity().findViewById(R.id.list);
adapter = new ItemListAdapter(getActivity(), getList(), 2);
progressDialog.show();
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            lv.setAdapter(adapter);
            progressDialog.dismiss();
            adapter.notifyDataSetChanged();
        }
    }, SPLASH_TIME_OUT);
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                userScrolled = true;
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (userScrolled) {
                userScrolled = false;
                progressDialog.getWindow().setGravity(Gravity.BOTTOM);
                progressDialog.show();
                updateListView();
            }
        }
    });

这是我的updateListView方法:

private void updateListView() {
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            if (appPreference.getLoadStatus().equals("0")) {
                for (int i = 0; i < getListMore().size(); i++) {
                    Log.i("MORE VALUES", "" + getListMore().get(i));
                    getList().add(getListMore().get(i));
                }
                appPreference.saveLoadStatus("1");
            }
            progressDialog.dismiss();
            adapter.notifyDataSetChanged();
        }
    }, 100);
}

这是我的getList()方法

public List<ItemList> getList() {
    ContentResolver cr = getActivity().getContentResolver();
    String where = Products.SYNCSTATUS + " != 3";
    Cursor c = cr.query(Products.BASEURI, null, where, null, Products.ID + " DESC");
    list = new ArrayList<ItemList>();
    try {
        if (c.getCount() > 0) {
            c.moveToFirst();
            do {
                int colN = c.getColumnIndex(Products.PRODUCT);
                int colP = c.getColumnIndex(Products.ON_HAND);
                int colI = c.getColumnIndex(Products.ID);
                int colD = c.getColumnIndex(Products.SKU);
                int colPr = c.getColumnIndex(Products.PRICE);
                int colUp = c.getColumnIndex(Products.UPDATE);
                String n = c.getString(colNad);
                String p = c.getString(colPle);
                long i = c.getLong(colIMEI);
                String d = c.getString(colDec);
                String pr = c.getString(colProd);
                String upd = c.getString(colUpdated);
                if (!Validating.areSet(upd))
                    upd = getString(R.string.strnever);
                if (tabletsize) {
                    list.add(new ItemList(n.toUpperCase(Locale.getDefault()), pr, p, i,
                            d, upd));
                } else {
                    list.add(new ItemList(getString(R.string.strproduct).toUpperCase(Locale.getDefault()) + ": " +
                            n.toUpperCase(Locale.getDefault()),
                            getString(R.string.strprice).toUpperCase(Locale.getDefault()) + ": " +
                                    pr.toUpperCase(Locale.getDefault()),
                            getString(R.string.strtotalqty).toUpperCase(Locale.getDefault()) + ": " + p, i,
                            getString(R.string.strsku).toUpperCase(Locale.getDefault()) + ": " + d,
                            getString(R.string.strlastsell).toUpperCase(Locale.getDefault()) + ": " + upd));
                }
            } while (c.moveToNext());
        } else {
            String n = getString(R.string.strnodata).toUpperCase(Locale.getDefault());
            String p = " ";
            long i = 0;
            String d = null;
            list.add(new ItemList(n, "", p, i, d));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return list;
}

我的意图是使用getListMore()方法加载更多项目,该方法从数据库加载数据,将它们添加到getList()方法中,该方法返回List<ItemList> 当我滚动列表视图没有加载新数据时,问题出现了,日志显示了 Log.i("MORE VALUES", "" + getListMore().get(i));从数据库返回数据,但listview上没有显示任何数据。 任何人都可以帮助解决为什么没有加载数据以及如何解决它的问题? 感谢

2 个答案:

答案 0 :(得分:0)

您实际上是在获取数据,使用getList()将其添加到列表中但不设置该列表。您可能需要使用新值setlist()。 类似于setList(new updated list here)然后adapter.notifyDataSetChanged()

您需要让getList()更加清晰,每次获得getList()并在其中添加新项目时,都会重新显示列表,添加您在第一次添加veru的旧项目。更好地改变这样的方法。

    public List<ItemList> getList() {
    if(list != null || list.size() > 0) {
      return list;
    }
        ContentResolver cr = getActivity().getContentResolver();
        String where = Products.SYNCSTATUS + " != 3";
        Cursor c = cr.query(Products.BASEURI, null, where, null, Products.ID + " DESC");
  list = new ArrayList<ItemList>();
                try {
            if (c.getCount() > 0) {
                c.moveToFirst();
                do {
                    int colN = c.getColumnIndex(Products.PRODUCT);
                    int colP = c.getColumnIndex(Products.ON_HAND);
                    int colI = c.getColumnIndex(Products.ID);
                    int colD = c.getColumnIndex(Products.SKU);
                    int colPr = c.getColumnIndex(Products.PRICE);
                    int colUp = c.getColumnIndex(Products.UPDATE);
                    String n = c.getString(colNad);
                    String p = c.getString(colPle);
                    long i = c.getLong(colIMEI);
                    String d = c.getString(colDec);
                    String pr = c.getString(colProd);
                    String upd = c.getString(colUpdated);
                    if (!Validating.areSet(upd))
                        upd = getString(R.string.strnever);
                    if (tabletsize) {
                        list.add(new ItemList(n.toUpperCase(Locale.getDefault()), pr, p, i,
                                d, upd));
                    } else {
                        list.add(new ItemList(getString(R.string.strproduct).toUpperCase(Locale.getDefault()) + ": " +
                                n.toUpperCase(Locale.getDefault()),
                                getString(R.string.strprice).toUpperCase(Locale.getDefault()) + ": " +
                                        pr.toUpperCase(Locale.getDefault()),
                                getString(R.string.strtotalqty).toUpperCase(Locale.getDefault()) + ": " + p, i,
                                getString(R.string.strsku).toUpperCase(Locale.getDefault()) + ": " + d,
                                getString(R.string.strlastsell).toUpperCase(Locale.getDefault()) + ": " + upd));
                    }
                } while (c.moveToNext());
            } else {
                String n = getString(R.string.strnodata).toUpperCase(Locale.getDefault());
                String p = " ";
                long i = 0;
                String d = null;
                list.add(new ItemList(n, "", p, i, d));
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return list;
    }

答案 1 :(得分:0)

你可以尝试改变:

new Handler().postDelayed(new Runnable() {

为:

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {

方法adapter.notifyDataSetChanged();只需要从主线程调用。