Swiperefresh listview, updates but not how you would expect

200 views Asked by At

I have a Listview that is using the swipefresh feature, and I've encountered a weird problem. When I swipe down, I see the animation and my information (updates behind the scenes.) However, I can't see the updated information, UNTIL I physically scroll down and back up again. The moment I scroll back up towards the top the old data is replaced with the new data.

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);
    }

What would or could cause this?

2

There are 2 answers

1
Lucas Crawford On BEST ANSWER

Looks like from my view of the code you have two adapters? mMainListAdapter and mAdapter? Since the data only appears when you scroll back, this means that you aren't properly notifying the adapter that you have updated the data. Call the notifyDatasetChanged() on the other adapter, since mMainListAdapter appears to be the main adapter.

0
Omar Mainegra On

You have to notify the adapter about the update with notifyDataSetChanged, or create a new adapter. This is on the method SwipeRefreshLayout.setOnRefreshListener. Follow this example:

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

Edit

Since your last update I can see that mMainListAdapter is your adapter. Then in your onPostExecute set the adapter to the ListView.

myListView.setAdapter(mMainListAdapter);

Repleace myListView with your ListView