Java 过滤器上的RecyclerView位置
我有一个RecyclerView,它显示CardView,其中包含来自SQLite数据库的一系列信息,如地址、lat、long等。当我在“活动”中引入一个过滤器以按地址\名称搜索时,它并没有完全正确地进行过滤。它确实按地址名称过滤,但其余信息(如lat long等)将从列表中的第一项显示,而不是从应该与地址名称对应的项显示 我尝试了以下操作(您将在onClickListener中看到),但也没有帮助:Java 过滤器上的RecyclerView位置,java,android,android-recyclerview,Java,Android,Android Recyclerview,我有一个RecyclerView,它显示CardView,其中包含来自SQLite数据库的一系列信息,如地址、lat、long等。当我在“活动”中引入一个过滤器以按地址\名称搜索时,它并没有完全正确地进行过滤。它确实按地址名称过滤,但其余信息(如lat long等)将从列表中的第一项显示,而不是从应该与地址名称对应的项显示 我尝试了以下操作(您将在onClickListener中看到),但也没有帮助: int adapterPosition = holder.getAdapterPosition
int adapterPosition = holder.getAdapterPosition();
你知道如何正确显示位置吗?
代码如下。谢谢你
SavedActivity.java
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String input = newText.toLowerCase();
ArrayList<String> newList = new ArrayList<>();
for (String addressname : DBaddress_name) {
if (addressname.toLowerCase().contains(input)) {
newList.add(addressname);
}
}
customerAdapter.updateList(newList);
return true;
}
}
class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private Activity activity;
private ArrayList<String> address_id, address_timestamp, address, address_lat, address_long, address_name;
private Animation recycler_anim;
CustomAdapter(Activity activity, Context context, ArrayList address_id, ArrayList address_timestamp, ArrayList address, ArrayList address_lat, ArrayList address_long, ArrayList address_name)
{
this.activity = activity;
this.context = context;
this.address_id = address_id;
this.address_timestamp = address_timestamp;
this.address = address;
this.address_lat = address_lat;
this.address_long = address_long;
this.address_name = address_name;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.address_id_txt.setText("Nº" + String.valueOf(address_id.get(position)));
holder.address_timestamp_txt.setText(String.valueOf(address_timestamp.get(position)));
holder.address_txt.setText(String.valueOf(address.get(position)));
holder.address_lat_txt.setText("Lat: " + String.valueOf(address_lat.get(position)));
holder.address_long_txt.setText("Long: " + String.valueOf(address_long.get(position)));
holder.address_name_txt.setText(String.valueOf(address_name.get(position)));
holder.recyclerViewLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPosition = holder.getAdapterPosition();
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id",String.valueOf(address_id.get(adapterPosition)));
intent.putExtra("address",String.valueOf(address.get(adapterPosition)));
intent.putExtra("address_name",String.valueOf(address_name.get(adapterPosition)));
activity.startActivityForResult(intent, 1);
}
});
}
@Override
public int getItemCount() {
return address_name.size(); //size method of any of the array - all the same
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView address_id_txt, address_timestamp_txt, address_txt, address_lat_txt, address_long_txt, address_name_txt, address_distance_txt;
LinearLayout recyclerViewLayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
address_id_txt = itemView.findViewById(R.id.address_id_txt);
address_timestamp_txt = itemView.findViewById(R.id.address_timestamp_txt);
address_txt = itemView.findViewById(R.id.address_address_txt);
address_lat_txt = itemView.findViewById(R.id.address_lat_txt);
address_long_txt = itemView.findViewById(R.id.address_long_txt);
address_name_txt = itemView.findViewById(R.id.address_name_txt);
address_distance_txt = itemView.findViewById(R.id.address_distance_txt);
recyclerViewLayout = itemView.findViewById(R.id.recyclerViewLayout);
//Animate RecyclerView
recycler_anim = AnimationUtils.loadAnimation(context, R.anim.recycler_anim);
recyclerViewLayout.setAnimation(recycler_anim);
}
}
public void updateList(ArrayList<String> newList){
address_name = new ArrayList<>();
address_name.addAll(newList);
notifyDataSetChanged();
}
}
@覆盖
公共布尔值onQueryTextSubmit(字符串查询){
返回false;
}
@凌驾
公共布尔onQueryTextChange(字符串newText){
字符串输入=newText.toLowerCase();
ArrayList newList=新的ArrayList();
for(字符串地址名称:DBaddress\u名称){
if(addressname.toLowerCase().contains(输入)){
newList.add(地址名);
}
}
customerAdapter.updateList(新列表);
返回true;
}
}
CustomAdapter.java
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String input = newText.toLowerCase();
ArrayList<String> newList = new ArrayList<>();
for (String addressname : DBaddress_name) {
if (addressname.toLowerCase().contains(input)) {
newList.add(addressname);
}
}
customerAdapter.updateList(newList);
return true;
}
}
class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
private Activity activity;
private ArrayList<String> address_id, address_timestamp, address, address_lat, address_long, address_name;
private Animation recycler_anim;
CustomAdapter(Activity activity, Context context, ArrayList address_id, ArrayList address_timestamp, ArrayList address, ArrayList address_lat, ArrayList address_long, ArrayList address_name)
{
this.activity = activity;
this.context = context;
this.address_id = address_id;
this.address_timestamp = address_timestamp;
this.address = address;
this.address_lat = address_lat;
this.address_long = address_long;
this.address_name = address_name;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.address_id_txt.setText("Nº" + String.valueOf(address_id.get(position)));
holder.address_timestamp_txt.setText(String.valueOf(address_timestamp.get(position)));
holder.address_txt.setText(String.valueOf(address.get(position)));
holder.address_lat_txt.setText("Lat: " + String.valueOf(address_lat.get(position)));
holder.address_long_txt.setText("Long: " + String.valueOf(address_long.get(position)));
holder.address_name_txt.setText(String.valueOf(address_name.get(position)));
holder.recyclerViewLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPosition = holder.getAdapterPosition();
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id",String.valueOf(address_id.get(adapterPosition)));
intent.putExtra("address",String.valueOf(address.get(adapterPosition)));
intent.putExtra("address_name",String.valueOf(address_name.get(adapterPosition)));
activity.startActivityForResult(intent, 1);
}
});
}
@Override
public int getItemCount() {
return address_name.size(); //size method of any of the array - all the same
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView address_id_txt, address_timestamp_txt, address_txt, address_lat_txt, address_long_txt, address_name_txt, address_distance_txt;
LinearLayout recyclerViewLayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
address_id_txt = itemView.findViewById(R.id.address_id_txt);
address_timestamp_txt = itemView.findViewById(R.id.address_timestamp_txt);
address_txt = itemView.findViewById(R.id.address_address_txt);
address_lat_txt = itemView.findViewById(R.id.address_lat_txt);
address_long_txt = itemView.findViewById(R.id.address_long_txt);
address_name_txt = itemView.findViewById(R.id.address_name_txt);
address_distance_txt = itemView.findViewById(R.id.address_distance_txt);
recyclerViewLayout = itemView.findViewById(R.id.recyclerViewLayout);
//Animate RecyclerView
recycler_anim = AnimationUtils.loadAnimation(context, R.anim.recycler_anim);
recyclerViewLayout.setAnimation(recycler_anim);
}
}
public void updateList(ArrayList<String> newList){
address_name = new ArrayList<>();
address_name.addAll(newList);
notifyDataSetChanged();
}
}
class CustomAdapter扩展了RecyclerView.Adapter{
私人语境;
私人活动;
私有数组列表地址\u id、地址\u时间戳、地址、地址\u lat、地址\u long、地址\u名称;
私人动画回收商_anim;
CustomAdapter(活动活动、上下文上下文、ArrayList地址\u id、ArrayList地址\u时间戳、ArrayList地址、ArrayList地址\u lat、ArrayList地址\u long、ArrayList地址\u名称)
{
这个。活动=活动;
this.context=上下文;
this.address\u id=地址\u id;
this.address\u timestamp=地址\u timestamp;
this.address=地址;
this.address\u lat=address\u lat;
this.address_long=address_long;
this.address\u name=地址\u name;
}
@非空
@凌驾
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图=充气机。充气(R.layout.my_行,父项,false);
返回新的MyViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull final MyViewHolder holder,final int position){
holder.address_id_txt.setText(“Nº”+String.valueOf(address_id.get(position));
holder.address\u timestamp\u txt.setText(String.valueOf(address\u timestamp.get(position));
holder.address_txt.setText(String.valueOf(address.get(position));
holder.address_lat_txt.setText(“lat:+String.valueOf(address_lat.get(position)));
holder.address_long_txt.setText(“long:+String.valueOf(address_long.get(position)));
holder.address\u name\u txt.setText(String.valueOf(address\u name.get(position));
holder.RecycleServiceWLayout.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int adapterPosition=holder.getAdapterPosition();
意向意向=新意向(上下文,UpdateActivity.class);
intent.putExtra(“id”,String.valueOf(address_id.get(adapterPosition));
intent.putExtra(“地址”,String.valueOf(address.get(adapterPosition));
intent.putExtra(“address_name”,String.valueOf(address_name.get(adapterPosition));
活动。startActivityForResult(意图,1);
}
});
}
@凌驾
public int getItemCount(){
返回地址_name.size();//任何数组的size方法-都一样
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
text查看地址\u id\u txt、地址\u时间戳\u txt、地址\u txt、地址\u lat\u txt、地址\u long\u txt、地址\u name\u txt、地址\u distance\u txt;
线性布局回收服务布局;
公共MyViewHolder(@NonNull View itemView){
超级(项目视图);
address\u id\u txt=itemView.findviewbyd(R.id.address\u id\u txt);
address_timestamp_txt=itemView.findViewById(R.id.address_timestamp_txt);
address\u txt=itemView.findviewbyd(R.id.address\u address\u txt);
address\u lat\u txt=itemviewbyd(R.id.address\u lat\u txt);
address\u long\u txt=itemviewbyd(R.id.address\u long\u txt);
address\u name\u txt=itemviewbyd(R.id.address\u name\u txt);
address\u distance\u txt=itemView.findviewbyd(R.id.address\u distance\u txt);
recyclerViewLayout=itemView.findViewById(R.id.recyclerViewLayout);
//设置回收视图的动画
recycler\u anim=AnimationUtils.loadAnimation(上下文,R.anim.recycler\u anim);
设置动画(回收器动画);
}
}
公共无效更新列表(ArrayList newList){
地址_name=new ArrayList();
地址_name.addAll(新列表);
notifyDataSetChanged();
}
}
您只是在传递地址名并在适配器内更新此列表,这就是其他值没有得到更新的原因,您还必须传递其他列表以正确更新它们。您需要创建一个POJO类,该类保存要在每个RecyclerView
项中显示的数据的字段;不需要将不同的列表传递给适配器,只需将此POJO的单个列表传递给RecyclerView;这将正确反映任何过滤,因为所有列表都合并到一个列表中
因此,您必须替换下面的
private ArrayList<String> address_id, address_timestamp, address, address_lat, address_long, address_name;
谢谢在onQueryTextChange中,实现这一点的最佳方法是什么?您好,谢谢,我现在已经将完整的适配器类和活动重新编写为POJO。这很简单。不过,还是不太确定如何过滤?@warfo09谢谢你的评论,我刚刚在更新部分更新了答案。。希望它能解决