Android - RecyclerView适配器和过滤器错误

时间:2017-10-12 09:14:47

标签: android android-recyclerview recycler-adapter

我创建了一个如下所示的RecyclerAdapter:

public ActiveSubsRecyclerAdapter(Context context, List<Subscription> activeSubsList) {
    this.mContext = context;
    this.activeSubsList = activeSubsList;

    this.activeFiltered = new ArrayList<>();
    this.activeFiltered = activeSubsList;
}

其中activeFiltered是包含过滤对象的List,最初包含整个列表。

然后我有一个filter()方法,如下所示:

  public void filter(final int category) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            //clear filteredActive
            activeFiltered.clear();
            switch (category) {
                case -1:
                    activeFiltered.addAll(activeSubsList);
                    break;
                case 0:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 0) activeFiltered.add(sub);
                    }
                    break;
                case 1:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 1) activeFiltered.add(sub);
                    }
                    break;
                case 2:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 2) activeFiltered.add(sub);
                    }
                    break;
                case 3:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 3) activeFiltered.add(sub);
                    }
                    break;
                case 4:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 4) activeFiltered.add(sub);
                    }
                    break;
                case 5:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 5) activeFiltered.add(sub);
                    }
                    break;
                case 6:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 6) activeFiltered.add(sub);
                    }
                    break;
                case 7:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 7) activeFiltered.add(sub);
                    }
                    break;
                case 8:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 8) activeFiltered.add(sub);
                    }
                    break;
                case 9:
                    for (Subscription sub : activeSubsList) {
                        if (sub.getCategory() == 9) activeFiltered.add(sub);
                    }
                    break;
            }
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    notifyDataSetChanged();
                }
            });
        }
    }).start();
}

适配器在主活动中初始化,如下所示:

private List<Subscription> activeSubs = new ArrayList<>();

adapter = new ActiveSubsRecyclerAdapter(this, activeSubs);
            recyclerView.setAdapter(adapter);

并过滤main_activity中的列表,如下所示:

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        switch (id) {
            case R.id.nav_a: adapter.filter(-1); break;
            case R.id.nav_b: adapter.filter(0); break;
            case R.id.nav_c: adapter.filter(1); break;
            case R.id.nav_d: adapter.filter(2); break;
            case R.id.nav_e: adapter.filter(3); break;
            case R.id.nav_f: adapter.filter(4); break;
            case R.id.nav_g: adapter.filter(5); break;
            case R.id.nav_h: adapter.filter(6); break;
            case R.id.nav_i: adapter.filter(7); break;
            case R.id.nav_j: adapter.filter(8); break;
            case R.id.nav_k: adapter.filter(9); break;
        }
}

问题在于,当我过滤时,不会显示任何元素。

我尝试了调试,并在两者上都得到了:

adapter = new ActiveSubsRecyclerAdapter(this, activeSubs);

以及任何过滤器案例(例如):

case R.id.nav_a: adapter.filter(-1); break;

activeSubsList中的adapter大小为3(正如预期的那样)。

当我在

上调试时
case -1: activeFiltered.addAll(activeSubsList); break;

RecyclerAdapter课程中,我认为activeSubsList的大小为0.这就是为什么列表没有显示的原因。

你可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

好的我认为问题在于这一行。

this.activeFiltered = activeSubsList;

此处您为列表分配了相同的内存位置,因此,如果您执行activeFiltered.clear();,则activeSubsList将被清除,

所以在ActiveSubsRecyclerAdapter构造函数上。通过.addAll()方法分配值。

activeFiltered.addAll(activeSubsList);

所以最后它看起来像

public ActiveSubsRecyclerAdapter(Context context, List<Subscription> activeSubsList) {
    this.mContext = context;
    this.activeSubsList = activeSubsList;

    this.activeFiltered = new ArrayList<>();
    this.activeFiltered.addAll(activeSubsList);
}

答案 1 :(得分:0)

private List<Subscription> activeSubs = new ArrayList<>();

adapter = new ActiveSubsRecyclerAdapter(this, activeSubs);
            recyclerView.setAdapter(adapter);

我认为问题就在这里你已经初始化了列表所以它不会是null,但你在列表中没有任何东西,你将它传递给适配器,所以适配器在列表中没有任何内容,你试图过滤空列表。可能会导致你这个问题。

<强>更新

activeFiltered = new ArrayList<>();
public ActiveSubsRecyclerAdapter(Context context, List<Subscription> activeSubsList) {
    this.mContext = context;

    this.activeFiltered = activeSubsList;
}

我已更新我的回答更改它并尝试,让我知道。