RecyclerView未使用notifyDataSetChanged进行更新

时间:2016-06-04 06:27:03

标签: java android android-recyclerview

我正在使用RecyclerView制作列表购物车。清除购物车后,RecyclerView仍显示最后一个列表购物车。我希望RecyclerView在购物车清理时不显示任何内容。我尝试过使用notifyDataSetChanged(),但它没有用。请帮帮我。

Tes4Activity.java

final Button buttonDeleteAll = (Button) findViewById(R.id.bClear);
        buttonDeleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAllListCart(Tes1Config.KD_MEJA);
                getData();
                total(Tes1Config.KD_MEJA);
                //finish();
                //Intent intent = new Intent(Tes4Activity.this, Tes4Activity.class);
                //startActivity(intent);
            }
        });




        private void getData(){
            class GetData extends AsyncTask<Void,Void,String>{
                ProgressDialog progressDialog;
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    progressDialog = ProgressDialog.show(Tes4Activity.this,
                    "Fetching Data", "Please wait...",false,false);
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    progressDialog.dismiss();
                    parseJSON(s);
                    //showData();
                    total(Tes1Config.KD_MEJA);
                }

                @Override
                protected String doInBackground(Void... params) {
                    BufferedReader bufferedReader = null;

                    try {
                        URL url = new URL(Tes1Config.CART_LIST_URL+Tes1Config.EXTURLZ+Tes1Config.KD_MEJA);
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        StringBuilder sb = new StringBuilder();

                        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                        String json;
                        while((json = bufferedReader.readLine())!= null){
                            sb.append(json+"\n");
                        }

                        return sb.toString().trim();

                    }catch(Exception e){
                        // Oops
                    }

                    return null;
                }
            }
            GetData gd = new GetData();
            gd.execute();
        }

        public void showData(){
          // if (Tes1Config.kd_menu == null){
            //   Toast.makeText(this, "please choose your menu!", Toast.LENGTH_LONG).show();
            //}

            //else {
               adapter = new  Tes2Adapter(Tes1Config.kd_menu, Tes1Config.nama, Tes1Config.harga, Tes1Config.jumlah,
                Tes1Config.subtotal, Tes1Config.total, Tes1Config.gambar, Tes1Config.gambars);
               if(recyclerView.getAdapter() == null){ //Adapter not set yet.
                   recyclerView.setAdapter(adapter);
               }
               else {
                   recyclerView.setAdapter(adapter);
                   //Already has an adapter
                   adapter.notifyDataSetChanged();
                   recyclerView.refreshDrawableState();
               }

               //recyclerView.setAdapter(adapter);
               //adapter.notifyDataSetChanged();
               //recyclerView.refreshDrawableState();
            }
        //}

        private void deleteAllListCart(String kd_meja){
            class RegisterUser extends AsyncTask<String, Void, String> {
                ProgressDialog loading;

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    loading = ProgressDialog.show(Tes4Activity.this, "Please Wait",null, true, true);
                }
                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    loading.dismiss();
                    Toast.makeText(Tes4Activity.this, s , Toast.LENGTH_LONG).show();
                    getData();
                }
                @Override
                protected String doInBackground(String... params) {
                    HashMap<String, String> data = new HashMap<String,String>();
                    data.put("kd_meja",params[0]);
                    RequestHandler rh = new RequestHandler();
                    String result = rh.sendPostRequest(Tes1Config.CART_DELETE_ALL_URL+Tes1Config.EXTURLZ+Tes1Config.KD_MEJA, data);
                    return  result;
                }
            }

            RegisterUser ru = new RegisterUser();
            ru.execute(kd_meja);
        }

Tes2Adapter

    public class Tes2Adapter extends RecyclerView.Adapter<Tes2Adapter.ViewHolder> {
        public List<ListItem2> items;
        Context context;
        Context mContext;
        private EditText editQuantity;
        String var_nama;
        String var_kdmeja;
        String var_kodemenu;
        String var_harga;
        Tes4Activity tes4Activity;
        private RecyclerView.Adapter adapter;
        private RecyclerView recyclerView;
        private Tes1Config tes1Config;
        String total;

       public Tes2Adapter(String[] kd_menu,String[] nama,String[] harga,
       String[] jumlah,String[] subtotal,String[] total,String[] gambar, Bitmap gambars[]){
            super();
            //kd_meja = Tes1Config.KD_MEJA;
            items = new ArrayList<ListItem2>();
            for(int i = 0; i<nama.length; i++){
                ListItem2 item = new ListItem2();
                item.setKd_menu(kd_menu[i]);
                item.setNama(nama[i]);
                item.setHarga(harga[i]);
                item.setJumlah(jumlah[i]);
                item.setSubtotal(subtotal[i]);
                item.setTotal(total[i]);
                item.setGambar(gambar[i]);
                item.setGambars(gambars[i]);
                items.add(item);
            }
        }

0 个答案:

没有答案