0

I am encountering a very strange problem My List is not refreshing well as the treatment go on The treatment seems to be stopping at the first 20 values then the list view keep refreshing the same values and same values Meanwhile when I am clearing the list I can see that for one row its treating well all the data but I don't have my full listview anymore

Can someone help me to figure why my listview still recycling the same view thought it's values should change

Thanks in advance for your help guys !!

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();
        }
user1159441
  • 123
  • 2
  • 7

1 Answers1

0

You're clearing the list whenever a new request is processed

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

Instead of creating the adapter every time a request is processed you should create it once, bind it with the ListView and just add the data whenevers appropriate.


1. Create the adapter before executing the requests, bind it with the ListView
2. Open a method in your adapter for adding a Stock that will also call notifyDataSetChanged after the items is added to the list of items
3. On onPostExecute add the Stock to the adapter with the newly created method. The item will be added to the list of items in the adapter and will notify about the dataset change

jankovd
  • 1,681
  • 1
  • 16
  • 22