首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swiperefresh listview,更新,但不是您期望的方式

Swiperefresh listview,更新,但不是您期望的方式
EN

Stack Overflow用户
提问于 2015-06-04 15:02:25
回答 2查看 175关注 0票数 0

我有一个Listview,它使用了swipefresh功能,我遇到了一个奇怪的问题。当我向下滑动时,我看到动画和我的信息(在幕后更新)。但是,直到我物理地向下滚动并再次向上滚动,才能看到更新的信息。当我向上滚动到顶部的时候,旧的数据就被新的数据所取代。

代码语言:javascript
复制
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);
    }

是什么导致了这一切?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-04 22:03:08

从我的角度看,你有两个适配器?mMainListAdapter和mAdapter?因为数据只有在回滚时才会出现,这意味着您没有正确地通知适配器您已经更新了数据。调用另一个适配器上的notifyDatasetChanged(),因为mMainListAdapter似乎是主适配器。

票数 0
EN

Stack Overflow用户

发布于 2015-06-04 15:09:28

您必须用notifyDataSetChanged通知适配器更新,或者创建一个新适配器。这是在方法SwipeRefreshLayout.setOnRefreshListener上的。遵循以下示例:

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

编辑

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

代码语言:javascript
复制
myListView.setAdapter(mMainListAdapter);

用你的myListView和你的ListView

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30647443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档