我在使用每行中包含edittext的回收站视图中使用搜索视图过滤器时遇到问题。通常,如果我不使用过滤器,我的edittext值不会在回收器视图中更改,但是当我使用搜索视图进行过滤时,edittext中的值不会保持不变。当我在搜索视图中键入内容时,所有值在编辑文本中都相同,它丢失在初始化期间添加的所有值,当我清除搜索视图时,edittext中的所有值变为相同或更改在每个4或5行之后重复的任何行值。
以下是我的代码:
public class MainActivity extends AppCompatActivity {
SearchView searchView;
RecyclerView recyclerView;
ListAdapterr listAdapterr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Info> listData = new ArrayList<>();
listData.add(new Info("Laptop",1));
listData.add(new Info("Laptop HP",10));
listData.add(new Info("Laptop Fujitsu",1));
listData.add(new Info("Laptop Lenovo",1));
listData.add(new Info("Laptop Acer",1));
listData.add(new Info("Laptop Stand",1));
listData.add(new Info("Laptop Cooler",3));
listData.add(new Info("Laptop Battery",1));
listData.add(new Info("Laptop HDD",1));
listData.add(new Info("Laptop RAM",1));
listData.add(new Info("Laptop Display",1));
listData.add(new Info("Laptop Cleaner",1));
listData.add(new Info("Laptop USB Webcam",1));
listData.add(new Info("Laptop USB Webcam2",1));
listData.add(new Info("Laptop USB Webcam3",1));
listData.add(new Info("Laptop USB Webcam4",1));
listData.add(new Info("Laptop USB Webcam5",1));
listData.add(new Info("Laptop USB Webcam6",1));
listData.add(new Info("Laptop USB Webcam7",150));
listData.add(new Info("Laptop USB Webcam8",1));
listData.add(new Info("Laptop USB Webcam9",1));
listData.add(new Info("Laptop USB Webcam10",100));
listData.add(new Info("Laptop USB Webcam11",1));
listData.add(new Info("Laptop USB Webcam12",1));
listData.add(new Info("Laptop USB Webcam13",1));
listData.add(new Info("Laptop USB Webcam14",1));
listData.add(new Info("Laptop USB Webcam15",25));
listData.add(new Info("Laptop USB Webcam16",1));
listData.add(new Info("Laptop USB Webcam17",1));
listData.add(new Info("Laptop USB Webcam19",1));
listData.add(new Info("Laptop USB Webcam22",1));
listData.add(new Info("Laptop USB Webcam21",1));
listData.add(new Info("Laptop USB Webcam23",1));
listData.add(new Info("Laptop USB Webcam24",1));
listData.add(new Info("Laptop USB Webcam25",1));
listData.add(new Info("Laptop USB Webcam232",1));
listData.add(new Info("Laptop USB Webcam234",1));
listData.add(new Info("Laptop USB Webcam123",1));
listData.add(new Info("Laptop USB Webcam332",1));
listData.add(new Info("Laptop USB Webcam223",1));
listData.add(new Info("Laptop USB Webcam112",1));
listData.add(new Info("Laptop USB Webcam3123",1));
listData.add(new Info("Laptop USB Webcam4123",1));
listData.add(new Info("Laptop USB Webcam3411",1));
listData.add(new Info("Laptop USB Webcam3222",1));
listData.add(new Info("Laptop USB Webcam3111",1));
listData.add(new Info("Laptop USB Webcam3333",1));
listData.add(new Info("Laptop USB Webcam4444",1));
listData.add(new Info("Laptop USB Webcam411",1));
listData.add(new Info("Laptop USB Webcam422",1));
listData.add(new Info("Laptop USB Webcam4223",1));
listData.add(new Info("Laptop USB Webcam41233",1));
searchView = (SearchView) findViewById(R.id.sview);
search(searchView);
recyclerView = (RecyclerView) findViewById(R.id.review);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
listAdapterr = new ListAdapterr(listData,R.layout.info_layout,this);
recyclerView.setAdapter(listAdapterr);
}
private void search(SearchView searchView) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
listAdapterr.getFilter().filter(newText);
return true;
}
});
}
}
适配器类:
public class ListAdapterr extends RecyclerView.Adapter<ListAdapterr.MListHolder> implements Filterable {
List<Info> mList;
List<Info> filteredResult;
int rowLayout;
Context context;
public class MListHolder extends RecyclerView.ViewHolder {
TextView tvitem;
EditText etquan;
public MListHolder(View v) {
super(v);
tvitem = (TextView) v.findViewById(R.id.item_name);
etquan = (EditText) v.findViewById(R.id.quantity);
}
}
public ListAdapterr(List<Info> mList, int rowLayout, Context context) {
this.mList = mList;
this.filteredResult = mList;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public ListAdapterr.MListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(rowLayout,parent,false);
return new MListHolder(v);
}
@Override
public void onBindViewHolder(final ListAdapterr.MListHolder holder, int position) {
final Info info = filteredResult.get(position);
holder.tvitem.setText(info.getItemName());
holder.etquan.setText(String.valueOf( info.getQuantity()));
holder.etquan.setTag(position);
holder.etquan.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) {
if(charSequence.length() > 0){
int q = Integer.parseInt(charSequence.toString());
Log.d("TAG","---------------->"+q);
info.setQuantity(q);
// holder.etquan.setText(q);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
public int getItemCount() {
return filteredResult.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String searchString = charSequence.toString();
if(searchString.isEmpty()){
filteredResult.removeAll(mList);
filteredResult.addAll(mList);
}
else
{
ArrayList<Info> filteredResultTwo = new ArrayList<>();
for(Info info : mList){
if(info.getItemName().toLowerCase().contains(charSequence)){
filteredResultTwo.add(info);
}
}
for(Info info: filteredResultTwo){
Log.d("TAGdata","Values------------------------>"+info.getQuantity());
}
filteredResult = filteredResultTwo;
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredResult;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredResult = (List<Info>) filterResults.values;
notifyDataSetChanged();
}
};
}
}