如何清除RecyclerView适配器数据 [英] how to clear RecyclerView adapter data

查看:251
本文介绍了如何清除RecyclerView适配器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的UI中,我使用了两个按钮将不同的数据加载到RecyclerView.首次单击每个按钮时,数据将正确显示.但是,如果我第二次单击该按钮,则数据将两次添加到适配器.我的意思是适配器未清除.单击按钮继续添加数据.我想我必须在单击按钮时对适配器做一些事情.任何人都可以让我知道如何清除适配器或我哪里出问题了.

Here in my UI, i have used two buttons to load different data to a RecyclerView. First time the data is displaying properly on click of each button. But if i click the button for the second time the data is adding to the adapter twice. I mean the the adapter is not cleared. it is keep on adding the data on click of button. I Think i have to do something with the adapter on click of a button. Can anyone pls let me know how to clear the adapter or where i am going wrong..

这是代码.

public class GstVendorLocRetrieve extends AppCompatActivity {

    private String vault;

    private TextView txt;
    public static final String DATA_URL = "http://oursite.com/getgstvendorlocation.php?vault_no=";
    public static final String DATA_URL1 = "http://oursite.com/getgstcustomerlocation.php?vault_no=";

    //Tags for my JSONRes
    public static final String TAG_VendorID = "VendorID";
    public static final String TAG_CustomerID = "Customer_ID";
    public static final String TAG_ADDRESS = "Address";

    private Button vendor;
    private Button customer;

    //Creating a List of superheroes
    private List<GstVendLoc> listSuperHeroes;
    private List<GstCustLoc> listSuperHeroes1;

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locationretrieve);

        SharedPreferences sharedPreferences = getSharedPreferences(GstLogin.SHARED_PREF_NAME, MODE_PRIVATE);
        vault = sharedPreferences.getString(GstLogin.EMAIL_SHARED_PREF,"Not Available");

        vendor = (Button) findViewById(R.id.login);
        customer = (Button) findViewById(R.id.login1);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        //Initializing our superheroes list
        listSuperHeroes = new ArrayList<>();
        listSuperHeroes1 = new ArrayList<>();

        vendor.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                recyclerView.setAdapter(null);
                getData();
            }
        });

        customer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                recyclerView.setAdapter(null);
                getData1();
            }
        });

    }

    //This method will get data from the web api
    private void getData(){
        //Showing a progress dialog
        final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);

        //Creating a json array request
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL+vault,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //Dismissing progress dialog
                        loading.dismiss();

                        //calling method to parse json array
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(jsonArrayRequest);
    }

    //This method will parse json data
    private void parseData(JSONArray array){
        for(int i = 0; i<array.length(); i++) {
            GstVendLoc gst1 = new GstVendLoc();
            JSONObject json = null;
            try {

                json = array.getJSONObject(i);
                gst1.setVendorID(json.getString(TAG_VendorID));
                gst1.setAddress(json.getString(TAG_ADDRESS));


            } catch (JSONException e) {
                e.printStackTrace();
            }
            listSuperHeroes.add(gst1);
        }

        //Finally initializing our adapter
        adapter = new CardAdapter17(listSuperHeroes, this);

        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);

    }

    private void getData1(){
        //Showing a progress dialog
        final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);

        //Creating a json array request
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL1+vault,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //Dismissing progress dialog
                        loading.dismiss();

                        //calling method to parse json array
                        parseData1(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(jsonArrayRequest);
    }

    //This method will parse json data
    private void parseData1(JSONArray array){
        for(int i = 0; i<array.length(); i++) {
            GstCustLoc gst1 = new GstCustLoc();
            JSONObject json = null;
            try {

                json = array.getJSONObject(i);
                gst1.setCustomer_ID(json.getString(TAG_CustomerID));
                gst1.setAddress(json.getString(TAG_ADDRESS));


            } catch (JSONException e) {
                e.printStackTrace();
            }
            listSuperHeroes1.add(gst1);
        }

        //Finally initializing our adapter
        adapter = new CardAdapter18(listSuperHeroes1, this);

        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }

}

推荐答案

您需要在第二次获取数据之前清除Array List.

You need to clear your Array List before you get data second time.

for loop之前在parseData1方法中执行此操作.

Do this inside parseData1 method before for loop.

listSuperHeroes.clear();

listSuperHeroes1.clear();

这篇关于如何清除RecyclerView适配器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆