arsenallavigne
arsenallavigne

Reputation: 141

How to clear ListView/prevent it from duplicating Android?

I'm trying to prevent my data from generating again in ListView. I've tried many examples that I seen online such as adapter.clear or listView.setAdapter(null).

It could be because I intent the data to the ListView so every time I click on the Cluster Marker, it will keep generating. I'm trying to prevent it from generating again by clearing the adapter but it doesn't work. I will post my codes and screenshots of it so it is easier to understand my problem. It's a logic problem but I can't solve it. Can anybody help/guide me through this?

MyListFragment.java

  public class MyListFragment extends ListFragment implements OnItemClickListener {

        private ArrayAdapter adapter;
        private List<String> location; 

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.list_item_location, container, false);
        }


        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            location = new ArrayList<>();
            adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
    //        List<String> location = getActivity().getIntent().getStringArrayListExtra("clusterData");
    //        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
    //        setListAdapter(adapter);
            test();
            getListView().setOnItemClickListener(this);
        }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String titleName = (String) parent.getItemAtPosition(position);
        Log.d("listitem", titleName);
        Intent intent = new Intent(getActivity(), Another.class);
        intent.putExtra(EXTRA_NAME, titleName);
        startActivity(intent);
    }

        private void test() {
            location = getActivity().getIntent().getStringArrayListExtra("clusterData");
            adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
            setListAdapter(adapter);
    //        if (location.isEmpty()) {
    //            location = getActivity().getIntent().getStringArrayListExtra("clusterData");
    //            adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
    //            setListAdapter(adapter);
    //        } else {
    //            adapter.clear();
    //            adapter.notifyDataSetChanged();
    //        }
    //        adapter.clear();
    //        location.clear();
    //        location = new ArrayList();
        }




    }

LocationRemitActivity.java

        private ArrayList<String> location;
       location = new ArrayList<>()

        mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<StoreLatLng>() {
            @Override
            public boolean onClusterClick(final Cluster<StoreLatLng> cluster) {
//                BottomSheetDialogFragment.getInstance();
                bottomSheetDialog.show(getSupportFragmentManager(), "Custom Bottom Sheet");
                for (StoreLatLng markersInsideCluster : cluster.getItems()) {
                    location.add(markersInsideCluster.getTitle());
                }

                //Convert string array to a collection
                getIntent().putStringArrayListExtra("clusterData", location);
            location = new ArrayList<>();
                return true;
            }
        });

        // Add cluster items (markers) to the cluster manager.
        addItems();
    }

enter image description here

Clicking the cluster marker will generate a list.

enter image description here

Clicking it again will generate twice

enter image description here

Upvotes: 0

Views: 250

Answers (3)

Shrikant
Shrikant

Reputation: 589

No need to clear adapter. Clear location list before add any new item in list.

mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<StoreLatLng>() {
            @Override
            public boolean onClusterClick(final Cluster<StoreLatLng> cluster) {
                //BottomSheetDialogFragment.getInstance();
                bottomSheetDialog.show(getSupportFragmentManager(), "Custom Bottom Sheet");
                **if(location != null && !location.isEmpty()) {
                   location.clear();
                   adapter.notifyDataSetChanged();
                }**
                for (StoreLatLng markersInsideCluster : cluster.getItems()) {
                    location.add(markersInsideCluster.getTitle());
                }

                //Convert string array to a collection
                getIntent().putStringArrayListExtra("clusterData", location);
            location = new ArrayList<>();
                return true;
            }
        });

Upvotes: 2

Pravin Fofariya
Pravin Fofariya

Reputation: 346

       Try this code in your test() method

        location.clear();  
        if(adapter!=null && location!=null)
        {
          adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
          setListAdapter(adapter);

        }

       location = getActivity().getIntent().getStringArrayListExtra("clusterData");
       adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
                        setListAdapter(adapter);

Upvotes: 1

Android Geek
Android Geek

Reputation: 9225

just add

 if(location!=null){
    location.clear();}

before adding data to the list. check the below code:

   private void test() {
if(location!=null){
location.clear();}
                location = getActivity().getIntent().getStringArrayListExtra("clusterData");

                adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
                setListAdapter(adapter);
        //        if (location.isEmpty()) {
        //            location = getActivity().getIntent().getStringArrayListExtra("clusterData");
        //            adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, location);
        //            setListAdapter(adapter);
        //        } else {
        //            adapter.clear();
        //            adapter.notifyDataSetChanged();
        //        }
        //        adapter.clear();
        //        location.clear();
        //        location = new ArrayList();
            }

Upvotes: 2

Related Questions