Swiperefresh listview,更新但不是你所期望的

Swiperefresh listview, updates but not how you would expect

我有一个使用 swipefresh 功能的 Listview,我遇到了一个奇怪的问题。当我向下滑动时,我会看到动画和我的信息(幕后更新。)但是,我不能看到更新的信息,直到我向下滚动并再次向上滚动。当我向上滚动到顶部时,旧数据将被新数据替换。

swipe//


     swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        swipeLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light);
        final TextView rndNum = (TextView) findViewById(R.id.timeStamp);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i("Refreshing", " onRefresh called from SwipeRefreshLayout");

                initiateRefresh();
            }
        });

Asynctask//

private void initiateRefresh() {
    Log.i("iRefreshing", "initiateRefresh");

    /**
     * Execute the background task, which uses {@link android.os.AsyncTask} to load the data.
     */
    new bgRefresh().execute();
}

private class bgRefresh extends AsyncTask<Void, Void, Boolean> {

    static final int TASK_DURATION = 3 * 1000; // 3 seconds

    @Override
    protected Boolean doInBackground(Void... params) {
        // Sleep for a small amount of time to simulate a background-task
        try {


     Thread.sleep(TASK_DURATION);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ArrayList<String> blah = new ArrayList<>();

            try {
                // Simulate network access
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat month_date = new SimpleDateFormat("M");
                String monthName = month_date.format(cal.getTime());
                int lastDayOfMonth =(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                int currentYear = Calendar.getInstance().get(Calendar.YEAR);
                int year = Calendar.getInstance().get(Calendar.YEAR);                ;
                Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);

                cal.set(Calendar.DAY_OF_MONTH,1);
                monthName = month_date.format(cal.getTime());
                //String test = cal.getTime();
                //start_date_monthly_statements=5/1/15&end_date_monthly_statements=5/27/15&activity_detail_type=all&reg_captcha=&display=
                String startDateRecentActivity = monthName + "/1/" + currentYear;
                String enddateRecentyActivity = monthName + "/" + lastDayOfMonth + "/" + currentYear;
                ///second calendar
                Calendar cal2 = Calendar.getInstance();

                cal2 = Calendar.getInstance();
                // cal.add(Calendar.DAY_OF_MONTH, -5); //Go back to the previous month


                webCreate web = new webCreate();
                try {

                    //Clear and go.
                    Fields.clear();
                    Values.clear();


                    Fields.add("data1");
                    Values.add(datav1");

                    cal = Calendar.getInstance();
                    month_date = new SimpleDateFormat("MM-dd-yyyy hh:mma");
                    String date = month_date.format(cal.getTime());
                    date = date.replace("-", "/");
                    date = date.replace(" ", "\n");

                    refreshTS = (TextView) findViewById(R.id.timeStamp);
                    refreshTS.setText(date);

                    mMainListAdapter = new CustomListViewAdapter(getApplicationContext(), Fields, Values);
                    mAdapter.n

                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }

                Thread.sleep(2000);
            } catch (InterruptedException e) {
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

                mAdapter.notifyDataSetChanged();
                // Tell the Fragment that the refresh has completed
                onRefreshComplete(result);
        }

    }

    private void onRefreshComplete(Boolean result) {
        Log.i("LOG_TAG", "onRefreshComplete");

        // Remove all items from the ListAdapter, and then replace them with the new items
        //mListAdapter.clear();
        //for (String cheese : result) {
        //    mListAdapter.add(cheese);
        //}

        // Stop the refreshing indicator
        swipeLayout.setRefreshing(false);
    }

什么会或可能导致这种情况?

您必须使用 notifyDataSetChanged 将更新通知适配器,或者创建一个新的适配器。这是在方法 SwipeRefreshLayout.setOnRefreshListener 上。按照这个例子:

https://www.bignerdranch.com/blog/implementing-swipe-to-refresh/

Edit

自从您上次更新后,我可以看到 mMainListAdapter 是您的适配器。然后在您的 onPostExecute 中将适配器设置为 ListView。

myListView.setAdapter(mMainListAdapter);

myListView 替换为您的 ListView

从我的代码来看,您似乎有两个适配器? mMainListAdapter 和 mAdapter?由于数据仅在您向后滚动时出现,这意味着您没有正确通知适配器您已更新数据。在另一个适配器上调用 notifyDatasetChanged(),因为 mMainListAdapter 似乎是主适配器。