4

I get the entire data from the Server by using doInBackground() method as shown below.

class DataLoader extends Activity{

    public void onCreate()
    {
         ...............................
          new AsyncTask1().execute();
    }

    class AsyncTask1 extends AsyncTask<String, String, String> {


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progressDialog = new ProgressDialog(DataLoader.this);
                progressDialog.setMessage("Loading...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            }


            protected String doInBackground(String... args) {

                JSONObject json;

                List<NameValuePair> params = new ArrayList<NameValuePair>();

                    params.add(new BasicNameValuePair("param1",datafield1));
                    params.add(new BasicNameValuePair("param2",datafield2));
                    json = jsonParser.makeHttpRequest(url, "POST", params);

                try {

                    int success = json.getInt(SUCCESS);

                    if (success == 1) {

                        products = json.getJSONArray(PRODUCTS);

                        // looping through All Products
                        for (int i = 0; i < products.length(); i++) {
                            JSONObject c = products.getJSONObject(i);

                            // Storing each json item in variable
                            String id = c.getString(ID);
                            String price = c.getString(PRICE);
                            String name = c.getString(NAME);


                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(PID, id);
                            map.put(PRICE, price);
                            map.put(NAME, name);

                           ..................

                            // adding HashList to ArrayList
                            productsList.add(map);

                        }
                        return "success";
                    }
                    else {
                        // no materials found for this section

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }


            protected void onPostExecute(String msg) {

                if( msg != null && msg.equals("success"))
                {

                progressDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */

                        customadapter=new CustomAdapterList(DataLoader.this, productsList);        
                        listView.setAdapter(adapter);

                    }
                });

                }
            }
        }

As per the above code, I am setting the data to the listview in the onPostExecute() method only after the entire data is loaded. But now I want to implement the CW Endless Adapter with the present code, but as I am new to this, I am unable to get how to move on from here. I included the CWAdapter jar file in the libs folder. Have referred this and searched a lot , but no use. Can some one please help me implementing the endless feature for the data I get?

Apparatus
  • 411
  • 1
  • 5
  • 19
  • What is your exact problem? Implenting a custom adapter is very simple. – rekire Feb 09 '13 at 06:27
  • My question is not about custom adapter. I have done the custom adapter already. But I want to add Endless Adapter feature to it. Moreover is there a way to get the data from the server and simultaneously set the data to the listview by using endless adapter rather than waiting for the entire data to be loaded. – Apparatus Feb 09 '13 at 06:30

1 Answers1

3

Basic Idea is to run an AsyncTask when more data is required, that is when uses scrolls to bottom of the list. Here's a quick demo project.

S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Thank you very much. This seems good. But I am unable to understand with the details you provided. Can you please elaborate? – Apparatus Feb 09 '13 at 06:47
  • Thank you very much. I will try this. BTW you mentioned this as //---paged query for specific set....... in your code. But I wrote the php code to return the entire data what's there in the database. Can you please point me to some links where I can get this paged query examples in php? Thanks. – Apparatus Feb 09 '13 at 08:25
  • @user2056532 There are [many examples](http://www.tutorialspoint.com/php/mysql_paging_php.htm) on [the Internet](http://www.google.com/search?q=php+paging). – S.D. Feb 09 '13 at 09:08
  • I am stuck at this ArrayAdapter class badly. How to prepare that class? I know how to make customAdapter by extending BaseAdapter. But what's this? Can you please reply me? – Apparatus Feb 15 '13 at 09:50
  • @Sam Extend `ArrayAdapter` where K is the class of objects you want to hold in adapter. Updated answer. – S.D. Feb 15 '13 at 09:55
  • please look at this small [code snippet](http://pastebin.com/kEUy47Fk). This is the way I have to do it. Right? Please confirm. And how to access the result arraylist in adapter to set the each arraylist item to the views? I know I have been pestering you, but I have to submit this academic project to my university in two days :( Please do me this last help. – Apparatus Feb 15 '13 at 13:29
  • @Sam I'd make a demo project. – S.D. Feb 15 '13 at 14:53
  • 1
    Thank you so much. You are my hero. – Apparatus Feb 18 '13 at 05:20
  • I was struggling with this since two days. Your demo project helped a lot, and now my adapter is running nicely. thanks! – shaktiman_droid Dec 18 '13 at 00:52
  • Please provide more information than just a link as links sometimes do not work but a simple explanation here would have been of a lot of help. Thanks – Kaveesh Kanwal Jul 04 '15 at 07:39