无尽的适配器 从 Web 服务获取数据



这是我的主要类

public class EndlessAdapterDemo extends Activity  {
    private ListView mListView;
    private ProgressDialog dialog;
    private OutletsXmlHandler mOutletXmlHandler;
    private FetchAndParseData getDownloadedData;
    private OutletListAdapter mOutletListAdapter;
    public InputStream inputStream;
    private ArrayList<Outlets> outletList;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.list_tab);

    mListView = (ListView) findViewById(R.id.listView1);
    mOutletXmlHandler = new OutletsXmlHandler();
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading");
    dialog.setCancelable(false);
    new MyTask().execute();  
  }
}

    **And in MyTask class i am doing following**
        public class MyTask extends AsyncTask<Void, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog.show();
            }
        @Override
        protected Void doInBackground(Void... params) {
            try {                           
                getDownloadedData = (FetchAndParseData) new FetchAndParseData().execute("http://184.106.222.195/zootapi/index.php/api/outlets/searchoutlets/token/something/cityid/83/parameters/&cu=American&s=Air$Conditioned");
                inputStream = getDownloadedData.get();
                XmlUtilities.parseAndLoadData(inputStream , mOutletXmlHandler);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }
        @Override
        protected void onPostExecute(Void result) {         
            super.onPostExecute(result);
            outletList = Project.getOutletList();//Project.getOutletList();             
            mListView.setAdapter(new DemoAdapter(outletList));
            if(dialog.isShowing()){
                dialog.dismiss();
            }
        }
    }

**Now in this Demo Adpapter i am extending it with EndlessAdpater**

 class DemoAdapter extends EndlessAdapter {
    private RotateAnimation rotate=null;
    DemoAdapter(ArrayList<Outlets> outletList) {
      super(new OutletListAdapter(EndlessAdapterDemo.this, outletList));
      rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                                  0.5f, Animation.RELATIVE_TO_SELF,
                                  0.5f);
      rotate.setDuration(600);
      rotate.setRepeatMode(Animation.RESTART);
      rotate.setRepeatCount(Animation.INFINITE);
    }
    @Override
    protected View getPendingView(ViewGroup parent) {
      View row=getLayoutInflater().inflate(R.layout.row, null);
      View child=row.findViewById(android.R.id.text1);
      child.setVisibility(View.GONE);
      child=row.findViewById(R.id.throbber);
      child.setVisibility(View.VISIBLE);
      child.startAnimation(rotate);
      return(row);
    }
    @Override
    protected boolean cacheInBackground() {
        try {                           
            getDownloadedData = (FetchAndParseData) new FetchAndParseData().execute("http://184.106.222.195/zootapi/index.php/api/outlets/fetchoutlets/token/something/cityid/76/verticalid/1");
            inputStream = getDownloadedData.get();
            XmlUtilities.parseAndLoadData(inputStream , mOutletXmlHandler);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      return true;
    }
    @Override
    protected void appendCachedData() {
        outletList = Project.getOutletList();//Project.getOutletList();             
        mListView.setAdapter(new DemoAdapter(outletList));
   }
  }

**现在在此源代码中,当列表到达末尾时,数据将从Web服务bt成功加载到列表视图中,以前的数据已丢失。

谁能帮帮我...我在这方面遇到了深深的麻烦**

您每次都会创建一个新的 DemoAdapter。

更改 appendCachedData 以获取包装的适配器,然后向其添加额外的元素:

@Override
protected void appendCachedData() {
    OutletListAdapter adptr = (OutletListAdapter) getWrappedAdapter();
    if (outletList!=null) {
        for (<T> addThis : outletList) {
            adptr.add(addThis);
        }
    }
}

最新更新