搜索recyclerview位置错误

时间:2018-10-19 09:49:32

标签: android search android-recyclerview position

当我搜索第一个项目时,就可以了。当我搜索第二或第三项等时,显示第二或第三项,但是当我单击时;更改android recyclerview的错误位置。单击时始终显示第一项数据。我指的是this website

CompanyListActivity,

@Override
public boolean onQueryTextChange(String newText) {
    final List<Company> filteredModelList = filter(companyList, newText);
    if (filteredModelList.size() > 0) {
        adapter.setFilter(filteredModelList);
        return true;
    } else {
        //Toast.makeText(MainActivity.this, "Not Found", Toast.LENGTH_SHORT).show();
        return false;
    }
}

private List<Company> filter(List<Company> companyList, String newText) {
    newText = newText.toLowerCase();
    final List<Company> filteredModelList = new ArrayList<>();
    for (Company model : companyList) {
        final String text = model.getCompanyName().toLowerCase();
        if (text.contains(newText)) {
            filteredModelList.add(model);
        }
    }
    companyList = db.getAllCompanies();
    adapter = new CompanyListRVAdapter(CompanyListActivity.this, filteredModelList);
    recycler_view.setLayoutManager(new LinearLayoutManager(CompanyListActivity.this));
    recycler_view.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    return filteredModelList;
}

CompanyListRVAdapter,

private Context context;
private List<Company> companyList;
private List<Company> mCompanyList;

public CompanyListRVAdapter(Context context, List<Company> companyList) {
    this.context = context;
    this.companyList = companyList;
}

private class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
    int position;

    public MyMenuItemClickListener(int pos) {
        this.position = pos;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_detail:
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                Intent intent = new Intent(context, CompanyDetailActivity.class);
                intent.putExtras(bundle);
                context.startActivity(intent);
                return true;
            default:
        }
        return false;
    }
}

public void setFilter(List<Company> companies) {
    mCompanyList = new ArrayList<>();
    mCompanyList.addAll(companies);
    notifyDataSetChanged();
}

0 个答案:

没有答案