简体   繁体   English

单击BottomSheet RecyclerView时清空ArrayList

[英]Empty ArrayList on clicking on BottomSheet RecyclerView

I have a MapsActivity with HeatMap on it. 我有一个带有HeatMap的MapsActivity。 Like Google Maps there is an option to filter data by various categories (like Satellite, Terrain etc in case of Google Maps). 像Google Maps一样,有一个选项可以按各种类别(例如Google Maps的卫星,地形等)过滤数据。

I have a button which brings a modal bottom sheet. 我有一个按钮,带一个模态底页。 This bottom sheet contains a recycler view with a list of categories. 该底部工作表包含带有类别列表的回收站视图。

When I click on a category and run a method to make an ArrayList of new filtered data the size turns out to be 0. 当我单击一个类别并运行一种方法以使新的过滤后的数据成为ArrayList时,大小变为0。

Method in MapsActivity class MapsActivity类中的方法

public void changeHeatMap(int category, int remove){
    int count = 0;
    if (remove==0) {
        ArrayList<LatLng> catHeatMap = new ArrayList<>(3);
        while (count < il_cat_data.size()) {
            if (il_cat_data.get(count) == category) {
                catHeatMap.add(list.get(count));
            }
            count++;
        }
        mProvider.setData(catHeatMap);
    }else{
        mProvider.setData(list);
    }
}

OnClick method in the RecyclerView Adapter class RecyclerView Adapter类中的OnClick方法

public void onClick(View view) {
        int position = getAdapterPosition();
        MapsActivity mapsActivity = new MapsActivity();
        if (position==MapsActivity.isClicked){
            MapsActivity.isClicked = -1;
            mapsActivity.changeHeatMap(position,1);
        }else{
            MapsActivity.isClicked = position;
            mapsActivity.changeHeatMap(position,0);
        }
    }

It throws this error 它抛出此错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.maps.android.heatmaps.HeatmapTileProvider.setData(java.util.Collection)' on a null object reference

While loop is not executed at all in changeHeatMap method and if I put a breakpoint and check size of il_cat_data then it comes out to be 0. while循环根本不会在changeHeatMap方法中执行,如果我放置一个断点并检查il_cat_data的大小,则结果为0。

I have checked that data is added in il_cat_data when the app is started. 我检查了启动应用程序时是否在il_cat_data中添加了数据。 The 'list' is an arraylist which contains complete data and is used to set up the first heatmap. “列表”是一个包含完整数据的数组列表,用于设置第一个热图。

What can be wrong here? 这里有什么问题? Is data not accessible because of the modal bottom sheet. 是否由于模态底页而无法访问数据。

This answer helped me. 这个答案帮助了我。

In the MapsActivity created a variable 在MapsActivity中创建一个变量

static MapsActivity mapsActivity;

In onCreate state: 在onCreate状态:

MapsActivity = this;

Added this method 添加了此方法

public static MapsActivity getInstance(){
    return   mapsActivity;
}

In the Recycler Adapter onClick method changed 在Recycler Adapter中,onClick方法已更改

MapsActivity mapsActivity = new MapsActivity();
mapsActivity.changeHeatMap(position,1);

to

MapsActivity.getInstance().changeHeatMap(position,1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM