我有一个RecylerView,它具有SearchView小部件以及包含复选框和播放器名称的项目列表。当我滚动项目并选择项目复选框时,没有问题。但是,当我按名称搜索项目然后选择它们时,就会出现问题,当我尝试搜索另一个项目时,先前的项目会被取消选中。
然后我从搜索视图中删除了BAT,您可以看到两个选中的球员都被取消了选择
CustomAdapter.java
public class CustomAdapter extends BaseAdapter implements Filterable {
ArrayList<SomeObject> someObjects= new ArrayList<SomeObject>();
ArrayList<String> name=new ArrayList<>();
ArrayList<String> id=new ArrayList<>();
ArrayList<String> role= new ArrayList<>();
ArrayList<String> price= new ArrayList<>();
ArrayList<String> teamName= new ArrayList<>();
CheckBox chk;
int totc=0;
int batc=0;
int maxbat=3;
int bwlc=0;
int maxbal=3;
int allc=0;
int maxall=4;
ValueFilter valueFilter;
int wktc=0;
int maxwkt=1;
Context c;
ArrayList<PlayersPool> players;
ArrayList<PlayersPool> checkdata;
ArrayList<PlayersPool> searchdata;
LayoutInflater inflater;
boolean[] itemChecked;
public CustomAdapter(Context c, ArrayList<PlayersPool> players) {
this.c = c;
this.players = players;
searchdata=players;
itemChecked= new boolean[players.size()];
}
@Override
public int getCount() {
return players.size();
}
@Override
public Object getItem(int position) {
return players.get(position);
}
@Override
public long getItemId(int position)
{
return players.indexOf(getItem(position));
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (inflater==null){
inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(convertView==null)
{
convertView=inflater.inflate(R.layout.model,null);
}
final TextView nameP= (TextView) convertView.findViewById(R.id.nameP);
TextView priceP= (TextView) convertView.findViewById(R.id.priceP);
final TextView roleP= (TextView) convertView.findViewById(R.id.roleP);
final TextView team= (TextView)convertView.findViewById(R.id.team_name);
chk= (CheckBox)convertView.findViewById(R.id.cb);
chk.setOnCheckedChangeListener(null);
chk.setFocusable(false);
nameP.setText(players.get(position).getName());
priceP.setText(players.get(position).getPrice());
roleP.setText(players.get(position).getRole());
team.setText(players.get(position).getTeamName());
if (players.get(position).isselected) {
chk.setChecked(true);
} else {
chk.setChecked(false);
}
checkdata= new ArrayList<>();
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if(b) {
players.get(position).isselected = true;
if (players.get(position).getRole().equals("BAT")) {
Toast.makeText(c,"Checked",Toast.LENGTH_SHORT).show();
batc++;
if (batc > 3) {
batc--;
totc--;
chk.setChecked(false);
players.get(position).isselected = false;
}
}
}
else {
players.get(position).isselected = false;
if(players.get(position).getRole().equals("BAT")){
batc--;
}
}
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<PlayersPool> filterList = new ArrayList<PlayersPool>();
for (int i = 0; i < searchdata.size(); i++) {
if ((searchdata.get(i).getRole().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
final PlayersPool play = new PlayersPool();
play.setName(searchdata.get(i).getName());
play.setTeamName(searchdata.get(i).getTeamName());
play.setRole(searchdata.get(i).getRole());
play.setPrice(searchdata.get(i).getPrice());
play.setIsselected(searchdata.get(i).isselected);
filterList.add(play);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = searchdata.size();
results.values = searchdata;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
players = (ArrayList<PlayersPool>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
您必须为此使用循环。
if (players.get(position).isselected) {
chk.setChecked(true);
for(PlayersPool item: searchdata){
if(players.get(position).getName().equals(item.getName())){
item.setIsselected(true);
}
}
} else {
chk.setChecked(false);
}