ListView在自定义Arrayadapter上无法正确刷新

时间:2014-01-06 23:43:42

标签: android performance

我遇到了一个非常奇怪的问题 随着治疗的继续,我的清单并不令人耳目一新 处理似乎在前20个值停止,然后列表视图保持刷新相同的值和相同的值 与此同时,当我清除列表时,我可以看到,对于一行,它可以很好地处理所有数据,但我不再拥有完整的列表视图

有人可以帮我解释为什么我的列表视图仍在回收相同的视图,认为它的值应该改变

提前感谢您的帮助!

public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";
    public static Map<String,StockIdentification> mapStockIdentification;
    ListView listStocksView; // Listview UI 
    List<Stock> stocks;      // My data 

    View rootView;ArrayAdapter<Stock> adapter;
    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
         rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
        //Stock stock = null;
        stocks = new ArrayList<Stock>();

        //Création d'une instance bdd Stockidentification
        StockIdentificationDao stockidDao = new StockIdentificationDao(getActivity().getApplicationContext());
        StockIdentificationInit stockidInit = new StockIdentificationInit(); 

        //getting my id object identification
        stockidInit.insertDAXValues(stockidDao);    
        List<StockIdentification>  stocksIdentification = new ArrayList<StockIdentification>(40);
        stocksIdentification  = stockidDao.getAllStockInfoDAXIncrease();
        mapStockIdentification = new HashMap<String, StockIdentification>();

        listStocksView = (ListView) rootView.findViewById(R.id.listStocks);

        try {
            for (StockIdentification stockID : stocksIdentification) {
                mapStockIdentification.put(stockID.getShortName(),stockID);
                new RequestParserID().execute(stockID.getShortName());//calling asyntask to realize the treatement 
            }
        }  catch (Exception e) {
            e.printStackTrace();
        }           

        return rootView;
    }

    class RequestParserID extends AsyncTask<String, Void, Stock>{
        private final String TAG = getClass().getSimpleName();

        private  final String URL = "http://MYSERVER";

         // Treatment getting my construction object 
        @Override
        protected Stock doInBackground(String... url) {
            return  stock = parse(doc);
        }

        // Clear the adapter to consider the lastest coming data stocked in List<Stock> stocks
        public void onPreExecute() {
            adapter = new StockArrayAdapter(getActivity(),stocks,mapStockIdentification);
            adapter.clear();
        }
        //refreshing my listview as the tretment is proceeeding 
        public void onPostExecute(final Stock stock) {

            super.onPostExecute(stock);

            //stocks.clear();// the row update well but i loose my list 
            stocks.add(stock);
            listStocksView.setAdapter(adapter);             
            adapter.notifyDataSetChanged();
        }

1 个答案:

答案 0 :(得分:0)

每当处理新请求时,您都会清除列表

public void onPreExecute() {
    adapter = new StockArrayAdapter(...); // a new adapter is created for every request
    adapter.clear();
}

不是每次处理请求时都创建适配器,而是应该创建一次,将其与ListView绑定,只需添加适当的数据。

1
。在执行请求之前创建适配器,将其与ListView绑定
2。在适配器中打开一个方法,添加Stock,在项目添加到项目列表后也会调用notifyDataSetChanged
3。在onPostExecute上使用新创建的方法将Stock添加到适配器。该项目将添加到适配器中的项目列表中,并将通知数据集更改