Pranesh Sahu
Pranesh Sahu

Reputation: 595

Listview doesn't get refreshed

I have an issue with the ListView that it doesn't refreshed on changing the Spinner value. What i did, Step 1: Created Custom Adapter and passes the the List to initialize my Adpater.

                list = handler.getdata(loc);
                if (list.size() > 0) {
                    ordList = new MicListAdapter(InventoryCount.this, list);
                    lstView.setAdapter(ordList);
                }

Step 2: Created one Spinner with seletion Listener

mspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {

                    spinlocation = mspinner.getItemAtPosition(arg2).toString();
                    loc = spinlocation;
                    String item = (String) arg0.getItemAtPosition(arg2);
                    ((TextView) arg0.getChildAt(0)).setTextColor(Color
                            .parseColor("#000000"));
                    ((TextView) arg0.getChildAt(0)).setTextSize(20);

                    if (!changeSpinner) {
                        new InflateList(spinlocation).execute();
                    } else {
                        changeSpinner = false;
                    }

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
                    new InflateList(spinlocation).execute();

                }
            });

Step 3: Once spinner's value changed it will call Asynctask where i'm loading the new list in the Listview but its not reflecting anything in ListView

class InflateList extends AsyncTask<String, String, List<MIC_OrderDetails>> {
        ProgressDialog dialog;
        Context context;
        String spinLoc;

        public InflateList(String spinloc) {
            dialog = new ProgressDialog(InventoryCount.this);
            dialog.setCancelable(false);
            this.spinLoc = spinloc;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.setMessage("Please wait while Data is Loading...");
            dialog.show();
        }

        @Override
        protected List<MIC_OrderDetails> doInBackground(String... params) {
            // TODO Auto-generated method stub
            String result = "";
            List<MIC_OrderDetails> lst = new ArrayList<MIC_OrderDetails>();
            try {

                lst = handler.getdata(spinLoc);

                result = "success";
                MIC_OrderDetails mic_OrderDetails = new MIC_OrderDetails();
                mic_OrderDetails.setResult(result);
                lst.add(mic_OrderDetails);

            }

            catch (Exception e) {
                result = "error";
                MIC_OrderDetails mic_OrderDetails = new MIC_OrderDetails();
                mic_OrderDetails.setResult(result);
                lst.add(mic_OrderDetails);
                dialog.dismiss();
                Log.e("Failed", e.getLocalizedMessage());
            }
            return lst;
        }

        protected void onPostExecute(List<MIC_OrderDetails> lst) {
            dialog.setMessage("Inflating Data...");

            if (lst.get(lst.size() - 1).getResult().contains(("success"))) {
                list.clear();
                ordList.clear();
                list.addAll(lst);

                // ordList.notifyDataSetInvalidated();
                ordList.notifyDataSetChanged();
                dialog.dismiss();

                /*
                 * ordList = new MicListAdapter(InventoryCount.this, lst);
                 * lstView.setAdapter(ordList);
                 * 
                 * dialog.dismiss();
                 */

            } else {
                dialog.dismiss();
                toastText.setText("Problem in loading Items");
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(toastLayout);
                toast.show();
            }
        }

    }

Upvotes: 0

Views: 54

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Listview doesn't get refreshed

Create a method in MicListAdapter for updating new data in current data-source of Adapter like:

In MicListAdapter class create a method addAll:

 public void addAll(List<MIC_OrderDetails> lst){
   this.list.clear();
   this.list.addAll(lst);
   this.notifyDataSetChanged();
  }

Where this.list is List object which is used to return count from getCount method.

and call addAll method using ordList from onPostExecute for updating ListView with latest data:

 ordList.addAll(lst);

Upvotes: 1

Related Questions