我有一个自定义适配器,扩展了Base适配器并实现了Filterable。当我写一些东西到searchview过滤器工作正常。当我单击列表视图的列表项进行过滤之前,已成功删除它。但是在过滤后,当我单击列表项时,它没有删除。我该如何解决这个问题?
适配器:
public class kisiler_adapter extends BaseAdapter implements Filterable{
private Context mContext;
private List<kisiler> kisilerList;
private List<kisiler> filteredList;
private CustomFilter filter;
private ArrayList<kisiler> filters;
public kisiler_adapter(@NonNull Context context, @NonNull List<kisiler> kisilerList) {
this.mContext=context;
this.kisilerList=kisilerList;
this.filteredList=kisilerList;
}
public void update(ArrayList<kisiler> results){
kisilerList=new ArrayList<>();
kisilerList.addAll(results);
notifyDataSetChanged();
}
class ViewHolder{
TextView isim_tv;
TextView numara_tv;
}
@Override
public int getCount() {
return kisilerList.size();
}
@Override
public Object getItem(int i) {
return kisilerList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder ;
if(convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.custom_kisiler_dialog_listview,parent,false);
holder=new ViewHolder();
holder.isim_tv=convertView.findViewById(R.id.tv_custom_kisiler_isim_dialog);
holder.numara_tv=convertView.findViewById(R.id.tv_custom_kisiler_numara_dialog);
// store the holder with the view
convertView.setTag(holder);
}else {
holder=(ViewHolder)convertView.getTag();
}
kisiler veri=kisilerList.get(position);
holder.isim_tv.setText(veri.getIsim());
holder.numara_tv.setText(veri.getNumara());
return convertView;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
if(filter == null)
{
filter=new CustomFilter();
}
return filter;
}
class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults=new FilterResults();
if(constraint!=null &&constraint.length()>0){
constraint=constraint.toString().toUpperCase();
filters=new ArrayList<>();
for(int i=0;i<filteredList.size();i++){
if(filteredList.get(i).getIsim().toUpperCase().contains(constraint)){
kisiler k=new kisiler(filteredList.get(i).getIsim(),filteredList.get(i).getNumara());
filters.add(k);
}
}
Toast.makeText(mContext,String.valueOf(filters.size()),Toast.LENGTH_SHORT).show();
filterResults.count=filters.size();
filterResults.values=filters;
}else {
filterResults.count=filteredList.size();
filterResults.values=filteredList;
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
kisilerList=(ArrayList<kisiler>)filterResults.values;
notifyDataSetChanged();
}
}
}
搜索视图:
//search view
void searchMethod(){
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setSubmitButtonEnabled(true);
searchView.setQueryHint("Search person..."); //search view
gridLayout.setVisibility(View.GONE);
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(list!=null &&list.size()>0){
listView.setVisibility(View.VISIBLE);
circleImageView.setVisibility(View.VISIBLE);
}else {
listView.setVisibility(View.GONE);
circleImageView.setVisibility(View.GONE);
}
}
});
搜索方法:
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
return false;
}
和列表视图提示事件:
public void listview_click(){
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, int position, long l) {
gridLayout.setVisibility(View.VISIBLE);
kisiler kisi=(kisiler)adapterView.getItemAtPosition(position);
Toast.makeText(getActivity(),kisi.getIsim(),Toast.LENGTH_SHORT).show();
grid_list.add(kisi);
list.remove(kisi);
adapter.notifyDataSetChanged();
if(list!=null &&list.size()>0){
listView.setVisibility(View.VISIBLE);
circleImageView.setVisibility(View.VISIBLE);
}else {
listView.setVisibility(View.GONE);
circleImageView.setVisibility(View.GONE);
}
if(grid_list!=null && grid_list.size()>0){
gridLayout.setVisibility(View.VISIBLE);
DynamicViews dynamicViews=new DynamicViews(getActivity());
TextView textView=dynamicViews.isim_text_view(getContext(),kisi.getIsim(),kisi.getNumara());
CircleImageView button=dynamicViews.iptal_buttonu(getContext());
gridLayout.addView(dynamicViews.linearLayout(getActivity(),textView,button),0);
if(grid_list!=null&&grid_list.size()>0&&gridLayout.getVisibility()==View.VISIBLE){
for( int i=0;i<gridLayout.getChildCount();i++){
final LinearLayout linearLayout=(LinearLayout)gridLayout.getChildAt(i);
final kisiler kisi2=grid_list.get(grid_list.size()-1-i);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gridLayout.removeView(linearLayout);
list.add(kisi2);
grid_list.remove(kisi2);
adapter.notifyDataSetChanged();
alfabetik_siralama();
if(list!=null &&list.size()>0){
listView.setVisibility(View.VISIBLE);
circleImageView.setVisibility(View.VISIBLE);
}else {
listView.setVisibility(View.GONE);
circleImageView.setVisibility(View.GONE);
}
}
});
}
}
}else {
gridLayout.setVisibility(View.GONE);
}
}
});
}