我的Android应用中有一个带有搜索栏的RecyclerView
。当我在其中搜索并单击某个项目时,它会显示错误的数据。
我知道它为什么会发生,但我不知道如何解决它。我尝试了很多东西,但我仍然遇到了问题。
这是我的适配器的代码。
RecyclerView.Adapter<hisAdapter.hisViewHolder> {
private ArrayList<hisItem> mhislist;
public onItemClickListener mListener;
public interface onItemClickListener {
void onItemClick(int position);
}
public void setOnitemClickLIstener(onItemClickListener lIstener) {
mListener = lIstener;
}
public class hisViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView1;
public TextView mTextView2;
public TextView card_num;
public hisViewHolder(View itemView) {
super(itemView);
mTextView1 = itemView.findViewById(R.id.textView1);
mTextView2 = itemView.findViewById(R.id.textView2);
card_num = itemView.findViewById(R.id.number);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
mListener.onItemClick(position);
}
}
}
});
}
}
public hisAdapter(ArrayList<hisItem> hisList){
mhislist = hisList;
}
@Override
public hisViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.his_item, parent, false);
hisViewHolder hvh = new hisViewHolder(v);
return hvh;
}
@Override
public void onBindViewHolder(hisViewHolder holder, int position) {
hisItem currentItem = mhislist.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.card_num.setText(currentItem.getText3());
}
@Override
public int getItemCount() {
return mhislist.size();
}
public void filterList(ArrayList<hisItem> filteredList){
mhislist = filteredList;
notifyDataSetChanged();
}
}
这是MainActivity
,我有搜索功能代码和点击处理程序。
editTextt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
@Override
public void afterTextChanged(Editable editable) {
filter(editable.toString());
}
});
}
public void filter(String text) {
ArrayList<hisItem> filteredList = new ArrayList<>();
for (hisItem item : hisList) {
if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
@Override
public void onItemClick(int position) {
Intent intent = new Intent(this, singleActivity.class);
hisItem clickeditem = hisList.get(position);
String pos = String.valueOf(position);
intent.putExtra(CARD_NUM ,pos );
intent.putExtra(HEAD_LINE,clickeditem.getText1());
intent.putExtra(REAL_NUM,clickeditem.getText3());
startActivity(intent);
}
如果我需要在我的问题中添加任何内容,请告诉我。我可以提供这些。感谢。
答案 0 :(得分:1)
您可以通过两种不同的方式解决问题。您可以考虑更改MainActivity
中的功能。因为你在onItemClick
函数中所做的事情是错误的。当hisList
中的某些项目已被过滤掉时,您会从hisList
获取该项目。因此,在这种情况下,您需要从filteredList
获取项目。通过在filteredList
中将MainActivity
声明为全局变量,可以轻松实现此目的。
// Declaring the filteredList as a public variable that can be accessed from anywhere in this class.
public ArrayList<String> filteredList = new ArrayList<>();
@Override
public void onItemClick(int position) {
Intent intent = new Intent(this, singleActivity.class);
// Get the item from the filteredList which is the updated one.
hisItem clickeditem = filteredList.get(position);
String pos = String.valueOf(position);
intent.putExtra(CARD_NUM ,pos );
intent.putExtra(HEAD_LINE,clickeditem.getText1());
intent.putExtra(REAL_NUM,clickeditem.getText3());
startActivity(intent);
}
但是,第二种方法是在适配器中集成onClick
行为。我更喜欢这个,因为这很简单,这不容易出错。它将始终处理传递给适配器的列表。所以没有其他头痛。
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Intent intent = new Intent(context, singleActivity.class);
hisItem clickeditem = mhislist.get(position);
String pos = String.valueOf(position);
intent.putExtra("CARD_NUMBER", pos);
intent.putExtra("HEAD_LINE", clickeditem.getText1());
intent.putExtra("REAL_NUMBER", clickeditem.getText3());
startActivity(intent);
}
});
希望有所帮助。
答案 1 :(得分:0)
使用此代码绝对可以解决您的问题!!
private ArrayList<Employee> filteredList = new ArrayList<Employee>();
private void filter(String text) {
filteredList.clear();
for (Employee item : mEmployeeList) {
if (item.getVehicle_number().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
myadapter.filterList(filteredList);
}
@Override
public void onItemClick(int position) {
Intent ediIntent = new Intent(this, Edit_Activity.class);
if(filteredList.size() > 0) {
Employee clickedItem = filteredList.get(position);
} else {
Employee clickedItem = mEmployeeList.get(position);
}
...
}