实现无限适配器

时间:2013-02-09 06:20:52

标签: android cwac-endless endlessadapter

我使用doInBackground()方法从服务器获取整个数据,如下所示。

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

                    }
                });

                }
            }
        }

根据上面的代码,我只在加载整个数据后才将数据设置为listview方法中的onPostExecute()。但是现在我想用现在的代码实现CW Endless Adapter,但是由于我是新手,我无法从这里开始。我在libs文件夹中包含了CWAdapter jar文件。已经提到this并进行了大量搜索,但没有用。有人可以帮我实现我得到的数据的无穷无尽的功能吗?

1 个答案:

答案 0 :(得分:3)

基本思路是在需要更多数据时运行AsyncTask,即使用滚动到列表底部时。这是一个quick demo project