0

I have a SwipeRefreshLayout with a listview, so when the user swipes it, I want the app to retrieve json data from a rest server (PHP), parse it, put the data inside the adapter and show the result inside the listview. My question is regarding the "retrieve json data from a rest server (PHP) as a result of swiping".

I read about SyncAdapter but as I understand, it works only inside a service?

Should the swiping call an ASyncTask to retrieve and parse the data? If so, how to connect it back to the adapter that is attached to the listview?

Any hint or examples will be very helpful, even a link to a site that can explain the process will do.

Here is the code I have so far:

View rootView = inflater.inflate(R.layout.s_feed, container, false);
sFeed = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
sFeed.setOnRefreshListener(this);
sFeed.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);

lvings = (ListView) rootView.findViewById(R.id.lvings);
???       
FeedItemAdapter feedItemAdapter = new FeedItemAdapter(R.layout.s_feed_item, feedItem);
lvings.setAdapter(feedItemAdapter);
???

and the refresh part:

@Override
public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override public void run() {
            getItems();
        }
    }, 5000);
}

private void getItems() {
    //call api using asynctask? how to connect to adapter?
    if (sFeed.isRefreshing()) sFeed.setRefreshing(false); <<< should be at the end of asynctask?
}
Amos
  • 1,321
  • 2
  • 23
  • 44

1 Answers1

0

Just use an AsyncTask and store the result you get in the onPostExecute into your adapter something like this:

    private FeedItemAdapter feedItemAdapter;


            @Override
            protected void onPostExecute(Object result) {
                super.onPostExecute(result);
                if(adapter == null){
                    feedItemAdapter = new FeedItemAdapter(R.layout.s_feed_item, feedItem);
                    listview.setAdapter(feedItemAdapter);
                } else {
                    adapter.clear();
                    adapter.addAll(result);
                    adapter.notifyDataSetChanged();
                }
            }

Edit you can just create a global variable like i did in the example and change FeedItemAdapter feedItemAdapter = new FeedItemAdapter(R.layout.s_feed_item, feedItem); to feedItemAdapter = new FeedItemAdapter(R.layout.s_feed_item, feedItem); because you declared the listview already as a global variable you don't have to pass it via the AsyncTask

Timmeeh93
  • 223
  • 1
  • 4
  • 14
  • I see 2 options: passing the listview to the asynctask and in onPostExecute I will create the adapter and attach it to the listview or I'll create an empty adapter, attach it to the listview and use it like you wrote. Regarding the second option: how can I make an empty adapter? it asks for a "content" when creating and passing null will throw an exception – Amos Jan 25 '15 at 21:57