我有一个带有CardView的RecycleView,它位于行中。单击任意行后,将打开一个与该行关联的新活动。一切正常,直到我将过滤器功能添加到此列表中为止。当我搜索列表然后单击一个项目时,它没有与过滤结果关联的功能。
示例:原始列表:A,B,C,D,E
搜索:“ B”个过滤结果:B
但是当我单击项目B时,它将在项目A中起作用 由于我有两个列表,一个是原始列表,另一个是存储过滤列表的列表,因此如何制作过滤列表对象,能够执行更新原始列表的功能/占据原始列表对象的位置。
searchdev.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
filter(editable.toString());
}});
public void filter (String search){
ArrayList<Devices> filteredList = new ArrayList<>();
for (Devices item : devicelist){
if (item.getDevicename().toLowerCase().contains(search.toLowerCase()))
{
filteredList.add(item);
}
}
DevAdapter.filterList(filteredList);
devicelist = filteredList;
}
点击功能:
DevAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() {
@Override
public void onReturnClick(int position) {
String renter = devicelist.get(position).getRenter();
final String childpos = String.valueOf(position);
if( name.equals(renter)) // sesuai renter
{
Toast.makeText(RentActivity.this,"Device RETURNED",Toast.LENGTH_SHORT).show();
devicedb.child(childpos).child("codereturn").setValue("-");
devicedb.child(childpos).child("statusdb").setValue("0");
devicedb.child(childpos).child("statuscard").setValue("Available");
devicedb.child(childpos).child("renter").setValue("-");
devicelist.get(position).changeOnReturn("-","Available");
DevAdapter.notifyItemChanged(position);
}
else
{
Toast.makeText(RentActivity.this,"Not Eligible for Returning",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRentClick(int position) {
String checkstats=devicelist.get(position).getStatuscard();
final String childpos = String.valueOf(position);
if(checkconnection(RentActivity.this)) {
if (checkstats.equals("Available")) {
devicedb.child(childpos).child("codereturn").setValue("-");
devicedb.child(childpos).child("statusdb").setValue("1");
devicedb.child(childpos).child("statuscard").setValue("Rented");
devicedb.child(childpos).child("renter").setValue(name);
Toast.makeText(RentActivity.this,"Device Rent Success",Toast.LENGTH_SHORT).show();
devicelist.get(position).changeOnRent(name,"Rented");
DevAdapter.notifyItemChanged(position);
} else {
Toast.makeText(RentActivity.this, "Device not Available to Rent", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(RentActivity.this, "NO INTERNET ACCESS", Toast.LENGTH_SHORT).show();
}
}
});
和适配器:
@Override
public int getItemCount() {
return devlist.size();
}
public void filterList(ArrayList<Devices> filteredlist){
devlist = filteredlist;
notifyDataSetChanged();
}
答案 0 :(得分:0)
因为我有2个数组列表,分别是“原始”列表和“已过滤”列表,所以我必须将“原始”位置转到“已过滤”列表中,以使过滤后的列表指向原始列表的正确位置。
所以我手动在orpos变量中添加数据的原始位置:
public Devices(String orpos,String devname, String codedev, String renter, String statusdb, String statuscard, String img) {
this.oriposition = orpos;
this.devicename = devname;
this.codereturn = codedev;
this.renter = renter;
this.statusdb = statusdb;
this.statuscard = statuscard;
this.img = img;
}
然后在“过滤器”功能中,我创建了包含已过滤列表的新适配器:
public void filter (final String search){
final ArrayList<Devices> filteredList = new ArrayList<>();
for (Devices item : devicelist){
if (item.getDevicename().toLowerCase().contains(search.toLowerCase()))
{
filteredList.add(item);
}
}
mRecyclerView = findViewById(R.id.devlist);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
DevAdapter = new ListAdapter(filteredList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(DevAdapter);
DevAdapter.setOnItemClickListener(new ListAdapter.OnItemClickListener() {
@Override
public void onReturnClick(int position) {
int positionnew = Integer.parseInt(filteredList.get(position).getOriposition());
String renter = devicelist.get(positionnew).getRenter();
final String childpos = String.valueOf(positionnew);
...
我创建一个名为positionnew的新变量,以使用原始列表数组编号覆盖过滤后的列表数组编号:
int positionnew = Integer.parseInt(filteredList.get(position).getOriposition());