Search code examples
androidandroid-arrayadaptercommonsware-cwac

Implementing EndlessAdapter with AsyncTask Passing in Object[]


I have reviewed all of the documentation for CWAC-endlessAdapteras well as the demo projects. I do understand how it works and mostly where everything goes. But I have a several questions on how to handle some things with how I am currently doing it now (I have yet to find any working example of this).

Here is a typical AsyncTask I use (cleaned up a bit):

class ReviewTask extends AsyncTask<String, String, Void> {


        @Override
        protected Void doInBackground(String... params) {

            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("username", userName));

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url_select);

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(param));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } catch (Exception e) {

            }
            try {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = "";
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();

            } catch (Exception e) {

            }
            return null;
    }

        protected void onPostExecute(Void v) {

            String review, newdate, item, rating, cat;
            try {
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    newdate = json_data.getString("date");
                    review = json_data.getString("review");
                    item = json_data.getString("item");
                    rating = json_data.getString("rating");
                    cat = json_data.getString("category");


                    reviews.add(review);
                    itemslist.add(item);
                    datelist.add(newdate);
                    ratings.add(rating);
                    cats.add(cat);

                }
            } 

            Profile[] p = new Profile[reviews.size()];
            int index = 0;

            for (String i : reviews) {
                p[index] = new Profile(reviews.get(index), datelist.get(index),
                        itemslist.get(index), ratings.get(index),
                        cats.get(index));
                index++;
            }

            if (getActivity() != null) {
                adapter = new ProfileAdapter(getActivity(), p);
                setListAdapter(adapter);
            }


        }
    }

In this task, I get all the data from MySQL Database via php. The SQL query I wrote gathers all data at once. Is this correct to still handle this way?

Also, I call this task in the onCreateView in my ListFragment. But it looks like the task needs to be called in cacheInBackground()?

Lastly, it sounds like I have to set the adapter in onActivityCreated like this:

// from Example
if (adapter==null) {
    items=new ArrayList<Integer>();

    for (int i=0;i<25;i++) { items.add(i); }

    adapter=new DemoAdapter(items);
    adapter.setRunInBackground(false); 

}
setListAdapter(adapter); 

I don't understand or see where there is a constructor for DemoAdapter(items), and based on the fact that I am passing an Array of Objects, would I do something like DemoAdapter(object[])? And it's ok if it is null, because gathering the data actually happens in the adapter, correct?

Last relevant note is, all of my adapters are in a class outside of the Fragment where they are set.


Solution

  • The SQL query I wrote gathers all data at once. Is this correct to still handle this way?

    That is up to you. However, doing it this way, you do not need EndlessAdapter, as you already have all your data. The point behind EndlessAdapter is to support situations where you do not "gather all data at once", but rather wish to gather a portion of the data, and gather another portion only when the user scrolls far enough.

    Also, I call this task in the onCreateView in my ListFragment. But it looks like the task needs to be called in cacheInBackground()?

    That is up to you. If you wish to use your own AsyncTask called whenever you want, that is fine. This is covered in the documentation:

    If you would prefer that EndlessAdapter not run its own AsyncTask, then call setRunInBackground(false). In this mode, your cacheInBackground() method will be called on the main application thread. It is up to you to arrange to do the work on your own background thread, then call onDataReady() when you want the adapter to update to reflect the newly added data. Note that appendCachedData() will not be used in this scenario.

     

    I don't understand or see where there is a constructor for DemoAdapter(items)

    There isn't one, as the demos did not require one.

    based on the fact that I am passing an Array of Objects, would I do something like DemoAdapter(object[])?

    That is up to you.

    And it's ok if it is null, because gathering the data actually happens in the adapter, correct?

    Again, that is up to you.

    However, as I pointed out earlier, since you do not need EndlessAdapter, I would recommend that you just stop using it.