我对Android编程比较陌生。我有一个RecycleView
,CardView
在其中排成行。单击任何行将打开与该行关联的新活动。在我将过滤器功能添加到此列表之前,一切都很顺利。当我搜索列表然后单击一个项目时,它不会打开与过滤结果相关联的活动。它会打开与原始列表中该位置的项目相关的活动。
示例 - 原始列表:AA,BA,CC,DA,ED,FF
搜索:' A'过滤结果:AA,BA,DA
但是当我点击项目DA时,它会打开CC的活动。我在适配器上调用了notifyDataSetChanged()
。
我搜索并发现了类似的问题,但无法在我的代码中实现。
以下是代码:
public class MainActivity extends AppCompatActivity implements ExampleAdapter.OnItemClickListener{
public static final String EXTRA_IMG = "imageresource";
public static final String EXTRA_TXT1 = "text1";
public static final String EXTRA_TXT2 = "text2";
public static final String EXTRA_TXT3 = "text3";
private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createExampleList();
buildRecyclerView();
EditText editText = findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
}
private void filter(String text) {
ArrayList<ExampleItem> filteredList = new ArrayList<>();
for (ExampleItem item : mExampleList) {
if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
private void createExampleList() {
//just creating list
}
private void buildRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(MainActivity.this);
}
@Override
public void onItemClick(int position) {
Intent detailintent = new Intent(this,DetailActivity.class);
ExampleItem clickedItem = mExampleList.get(position);
detailintent.putExtra(EXTRA_IMG,clickedItem.getImageResource());
detailintent.putExtra(EXTRA_TXT1,clickedItem.getText1());
detailintent.putExtra(EXTRA_TXT2,clickedItem.getText2());
detailintent.putExtra(EXTRA_TXT3,clickedItem.getText3());
startActivity(detailintent);
}
}
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener=listener;
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView7);
mTextView3 = itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mListener != null ){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
mListener.onItemClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,
parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mTextView3.setText(currentItem.getText3());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public void filterList(ArrayList<ExampleItem> filteredList) {
mExampleList = filteredList;
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
您有两个列表,一个在您的活动中,另一个在您的适配器中。
过滤列表后,您只需通知适配器并仅将适配器mExampleList
设置为新的已过滤列表。活动列表保持不变。
单击某个项目时,您将让该活动处理click事件。但是活动仍然有旧的,未经过滤的列表,因此它会将错误的数据发送到您的新活动。
解决方案:只需在过滤方法
中的行mExampleList = filteredList
旁边添加mAdapter.filterList(filteredList);
即可