I have a custom adapter extends Base adapter and implements Filterable. When i write something to searchview filter is working well. Before filtering when i click the list item of listview, it is removed successfully. But after filtering when i click the list item, its not removing . How can i fix this problem ?
1 1
2 2
3 3
4 4
adapter:
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 :
//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);
}
}
});
search methods:
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
return false;
}
and list view clisk events:
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);
}
}
});
}