implementing the Endless Adapter

一曲冷凌霜 提交于 2019-12-06 04:21:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/14785547/implementing-the-endless-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!