如何清除ListView /阻止它复制Android?

时间:2017-06-30 06:43:24

标签: android listview

我试图阻止我的数据再次在ListView中生成。我尝试了很多我在网上看过的例子,例如adapter.clearlistView.setAdapter(null)

可能是因为我将数据转移到ListView所以每次点击Cluster Marker时它都会继续生成。我试图通过清除适配器来阻止它再次生成,但它不起作用。我会发布我的代码和截图,以便更容易理解我的问题。这是一个逻辑问题,但我无法解决它。任何人都可以帮助/指导我吗?

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

点击群集标记会生成一个列表。

enter image description here

再次点击它会生成两次

enter image description here

3 个答案:

答案 0 :(得分:2)

只需添加

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

在将数据添加到列表之前。  检查以下代码:

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

答案 1 :(得分:2)

无需清除适配器。在列表中添加任何新项目之前清除位置列表。

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

答案 2 :(得分:1)

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