Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear RecyclerView adapter data

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..

Here is the code.

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

}
like image 732
sunil y Avatar asked Jan 25 '17 05:01

sunil y


2 Answers

Use this code for clear RecycleView items

public void clear() {
    int size = listSuperHeroes.size();
    listSuperHeroes.clear();
    notifyItemRangeRemoved(0, size);
}
like image 134
Rony Avatar answered Sep 25 '22 03:09

Rony


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

Do this inside parseData1 method before for loop.

listSuperHeroes.clear();

listSuperHeroes1.clear();
like image 36
Jay Rathod RJ Avatar answered Sep 23 '22 03:09

Jay Rathod RJ