列表视图无法刷新

时间:2016-07-18 13:59:05

标签: android

我对ListView有一个问题,即在更改Spinner值时它没有刷新。 我做了什么, 步骤1: 创建自定义适配器并传递List以初始化我的Adpater。

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

第2步: 用seletion Listener创建了一个Spinner

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

                }
            });

第3步: 一旦微调器的值改变,它将调用Asynctask,我在Listview中加载新列表,但它没有反映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();
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

  

Listview无法刷新

MicListAdapter中创建一个方法,用于更新适配器的当前数据源中的新数据,如:

MicListAdapter类中创建方法addAll:

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

其中this.list是List对象,用于从getCount方法返回计数。

并使用addAll中的ordList来调用onPostExecute方法,以便使用最新数据更新ListView:

 ordList.addAll(lst);