Swiperefresh列表视图,更新但不是您期望的方式

时间:2015-06-04 15:02:25

标签: android listview swiperefreshlayout

我有一个使用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);
    }

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

2 个答案:

答案 0 :(得分:0)

您必须使用notifyDataSetChanged通知适配器有关更新的信息,或者创建新的适配器。这是方法SwipeRefreshLayout.setOnRefreshListener。请遵循以下示例:

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

  

修改

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

myListView.setAdapter(mMainListAdapter);

使用ListView重复myListView

答案 1 :(得分:0)

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