Skip to main content
agregue mas detalles de otros errores
Origen Enlace
Geek
  • 357
  • 3
  • 18

¿Recyclerview se queda ¿Error en blanco al hacer un filtro de busquedarecyclerview (Android Studio)?

sucede que tengo un proyecto en android studio el cual realiza una búsqueda en un recyclerview desde un searchview, al iniciar ladepurar mi aplicacion me muestra todosveo que si filtra bien los datos pero al buscar no me muestra nada y todo se quedamostrar los datos filtrados en blancoel recyclerview muestra datos que no tienen que ver con la busqueda, aqui mi codigo:

public class InicioFragment extends Fragment {

    public RecyclerView recyclerView;
    public RecyclerView.Adapter adapter;
    public RecyclerView.LayoutManager layoutManager;
    public SearchView searchView;

    public Context context;
    public ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    public AppConfiguration appConfiguration;

    public List<Inicio> listCreditos;

    public InicioFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.rclvInicio);
        recyclerView.setHasFixedSize(true);

        searchView = (SearchView) rootView.findViewById(R.id.searchInicio);

        this.context = this.getContext();
        appConfiguration = (AppConfiguration) getActivity().getApplicationContext();

        arrayList = new ArrayList<InicioItem>();
      
        arrayList = GetArrayListInicioItem();
      
      adapter = new InicioRecyclerAdapter(context, arrayList);
      
      recyclerView.setAdapter(adapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
             ((InicioRecyclerAdapter) adapter).getFilter().filter(newText.toString());
                return true;
            }
        });

        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return rootView;
    }

}

Adapter

  

public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
    //private
    ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>(); 

    private ArrayList<InicioItem> inicioItemFilter;
    private ArrayList<InicioItem> filteredContactList;
    //private InicioFilterClassCustomFilter inicioFilterClass;mFilter;

    private LayoutInflater inflater;
    private Context context;
    AppConfiguration appConfiguration;
    Filtro filtro;
    public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.arrayList = arrayList;
        this.arrayListInicio = arrayList; 

        this.inicioItemFilter = arrayList;inicioItemFilter=arrayList;
        this.filteredContactList=arrayList;filteredContactList=new ArrayList<>();
        //this.filteredContactList.addAll(arrayList);
  inicioFilterClass      this.mFilter = new InicioFilterClassCustomFilter(arrayList, InicioRecyclerAdapter.this);
        
 appConfiguration = (AppConfiguration) context.getApplicationContext}
    @Override
    public Filter getFilter(); {
        return mFilter;
    }

    @Override
    public int getItemCount() {
        return inicioItemFilterfilteredContactList.size();
    }

    @Override
    public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.inicio_layoutitem_inicio_layout, parent, false);//LayoutInflater.from(parent.getContext()) --> inflate
        InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
        return inicioRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(InicioRecyclerViewHolder holder, int position) {
        InicioItem inicioItem = arrayList.get(position);
    }

    public//private voidclass setArrayList(ArrayList<InicioItem>Filtro arrayList)extends Filter{
        this.inicioItemFilter = arrayList;
    }

    @Override
 public class CustomFilter publicextends Filter getFilter() {
        if (filtro == null)
            filtro =private newInicioRecyclerAdapter Filtro();inicioRecyclerAdapter;
        return filtro;
    }

    /*public voidprivate filterListCustomFilter(StringInicioRecyclerAdapter textinicioRecyclerAdapter) {
        inicioFilterClass.filter(text);
       super();
    }

    @Override
    public Filter getFilter() {this.inicioRecyclerAdapter=inicioRecyclerAdapter;
        return arrayListInicio;
    }*/

private class Filtro extends Filter{

    @Override
    @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
                filteredContactList.clear();
                final FilterResults results = new FilterResults();
            final String text=charSequencetext = charSequence.toString().toLowerCase().trim(); 

                if (text != "" || text.length() != 0) { 

                    for (final InicioItem item : inicioItemFilter) {
                        if (item.nombrePersona().toLowerCase().contains(text)))
                           {
                            filteredContactList.add(item);
                    }

                    results.values = filteredContactList;
                    results.count = filteredContactList.size();
            }
        }
    else
            {else
                results.values{
 = inicioItemFilter;
                results.count = inicioItemFilterfilteredContactList.sizeaddAll(inicioItemFilter);
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
 
                inicioItemFilter = this.inicioRecyclerAdapter.notifyDataSetChanged(ArrayList<InicioItem>) filterResults.values;;
                notifyDataSetChanged();}
        }
    }

¿Recyclerview se queda en blanco al hacer un filtro de busqueda (Android Studio)?

sucede que tengo un proyecto en android studio el cual realiza una búsqueda en un recyclerview desde un searchview, al iniciar la aplicacion me muestra todos los datos pero al buscar no me muestra nada y todo se queda en blanco, aqui mi codigo:

adapter = new InicioRecyclerAdapter(context, arrayList);
        recyclerView.setAdapter(adapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

 @Override
 public boolean onQueryTextChange(String newText) {
           ((InicioRecyclerAdapter)adapter).getFilter().filter(newText.toString());
 return true;
 }
});

Adapter

 

public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
    //private
    ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>();
    private ArrayList<InicioItem> inicioItemFilter;
    private ArrayList<InicioItem> filteredContactList;
    //private InicioFilterClass inicioFilterClass;
    private LayoutInflater inflater;
    private Context context;
    AppConfiguration appConfiguration;
    Filtro filtro;
    public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.arrayList = arrayList;
        this.arrayListInicio = arrayList;
        this.inicioItemFilter = arrayList;
        this.filteredContactList=arrayList;
        //  inicioFilterClass = new InicioFilterClass(arrayList, this);
        appConfiguration = (AppConfiguration) context.getApplicationContext();
    }

    @Override
    public int getItemCount() {
        return inicioItemFilter.size();
    }

    @Override
    public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.inicio_layout, parent, false);
        InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
        return inicioRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(InicioRecyclerViewHolder holder, int position) {
        InicioItem inicioItem = arrayList.get(position);
    }

    public void setArrayList(ArrayList<InicioItem> arrayList) {
        this.inicioItemFilter = arrayList;
    }

    @Override
    public Filter getFilter() {
        if (filtro == null)
            filtro = new Filtro();
        return filtro;
    }

    /*public void filterList(String text) {
        inicioFilterClass.filter(text);
       
    }

    @Override
    public Filter getFilter() {
        return arrayListInicio;
    }*/

private class Filtro extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            filteredContactList.clear();
            final FilterResults results = new FilterResults();
            String text=charSequence.toString();
            if (text != "" || text.length() != 0) {
                for (final InicioItem item : inicioItemFilter) {
                    if (item.nombrePersona().toLowerCase().contains(text)))
filteredContactList.add(item);
                }

                results.values = filteredContactList;
                results.count = filteredContactList.size();
            }
            else
            {
                results.values = inicioItemFilter;
                results.count = inicioItemFilter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
 
                inicioItemFilter = (ArrayList<InicioItem>) filterResults.values;
                notifyDataSetChanged();
        }
    }

¿Error en filtro de recyclerview (Android Studio)?

sucede que tengo un proyecto en android studio el cual realiza una búsqueda en un recyclerview desde un searchview, al depurar mi aplicacion veo que si filtra bien los datos pero al mostrar los datos filtrados en el recyclerview muestra datos que no tienen que ver con la busqueda, aqui mi codigo

public class InicioFragment extends Fragment {

    public RecyclerView recyclerView;
    public RecyclerView.Adapter adapter;
    public RecyclerView.LayoutManager layoutManager;
    public SearchView searchView;

    public Context context;
    public ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    public AppConfiguration appConfiguration;

    public List<Inicio> listCreditos;

    public InicioFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.rclvInicio);
        recyclerView.setHasFixedSize(true);

        searchView = (SearchView) rootView.findViewById(R.id.searchInicio);

        this.context = this.getContext();
        appConfiguration = (AppConfiguration) getActivity().getApplicationContext();

        arrayList = new ArrayList<InicioItem>();
      
        arrayList = GetArrayListInicioItem();
      
      adapter = new InicioRecyclerAdapter(context, arrayList);
      
      recyclerView.setAdapter(adapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
             ((InicioRecyclerAdapter) adapter).getFilter().filter(newText.toString());
                return true;
            }
        });

        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return rootView;
    }

}

Adapter 

public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
    ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>(); 

    private ArrayList<InicioItem> inicioItemFilter;
    private ArrayList<InicioItem> filteredContactList;
    private CustomFilter mFilter;

    private LayoutInflater inflater;
    private Context context;
   
    public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.arrayList = arrayList;
        this.arrayListInicio = arrayList; 

        this.inicioItemFilter=arrayList;
        this.filteredContactList=new ArrayList<>();
        this.filteredContactList.addAll(arrayList);
        this.mFilter = new CustomFilter(InicioRecyclerAdapter.this);
        
    }
    @Override
    public Filter getFilter() {
        return mFilter;
    }

    @Override
    public int getItemCount() {
        return filteredContactList.size();
    }

    @Override
    public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_inicio_layout, parent, false);//LayoutInflater.from(parent.getContext()) --> inflate
        InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
        return inicioRecyclerViewHolder;
    }        

    
    //private class Filtro extends Filter{
    public class CustomFilter extends Filter {

        private InicioRecyclerAdapter inicioRecyclerAdapter;

        private CustomFilter(InicioRecyclerAdapter inicioRecyclerAdapter) {
            super();
            this.inicioRecyclerAdapter=inicioRecyclerAdapter;
        }

        @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                filteredContactList.clear();
                final FilterResults results = new FilterResults();
            final String text = charSequence.toString().toLowerCase().trim(); 

                if (text != "" || text.length() != 0) { 

                    for (final InicioItem item : inicioItemFilter) {
                        if (item.nombrePersona().toLowerCase().contains(text))
                           {
                            filteredContactList.add(item);
                    }

                    results.values = filteredContactList;
                    results.count = filteredContactList.size();
                }
                else
                {
                    filteredContactList.addAll(inicioItemFilter);
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            this.inicioRecyclerAdapter.notifyDataSetChanged();
            }
        }
    }
título editado
Enlace
Geek
  • 357
  • 3
  • 18

¿Filtro de busqueda ¿Recyclerview se queda en blanco al hacer un filtro de busqueda (Android Studio)?

Origen Enlace
Geek
  • 357
  • 3
  • 18

¿Filtro de busqueda se queda en blanco?

sucede que tengo un proyecto en android studio el cual realiza una búsqueda en un recyclerview desde un searchview, al iniciar la aplicacion me muestra todos los datos pero al buscar no me muestra nada y todo se queda en blanco, aqui mi codigo:

MainActivity

adapter = new InicioRecyclerAdapter(context, arrayList);
        recyclerView.setAdapter(adapter);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

 @Override
 public boolean onQueryTextChange(String newText) {
           ((InicioRecyclerAdapter)adapter).getFilter().filter(newText.toString());
 return true;
 }
});

Adapter

public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
    //private
    ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
    private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>();
    private ArrayList<InicioItem> inicioItemFilter;
    private ArrayList<InicioItem> filteredContactList;
    //private InicioFilterClass inicioFilterClass;
    private LayoutInflater inflater;
    private Context context;
    AppConfiguration appConfiguration;
    Filtro filtro;
    public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.arrayList = arrayList;
        this.arrayListInicio = arrayList;
        this.inicioItemFilter = arrayList;
        this.filteredContactList=arrayList;
        //  inicioFilterClass = new InicioFilterClass(arrayList, this);
        appConfiguration = (AppConfiguration) context.getApplicationContext();
    }

    @Override
    public int getItemCount() {
        return inicioItemFilter.size();
    }

    @Override
    public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.inicio_layout, parent, false);
        InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
        return inicioRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(InicioRecyclerViewHolder holder, int position) {
        InicioItem inicioItem = arrayList.get(position);
    }

    public void setArrayList(ArrayList<InicioItem> arrayList) {
        this.inicioItemFilter = arrayList;
    }

    @Override
    public Filter getFilter() {
        if (filtro == null)
            filtro = new Filtro();
        return filtro;
    }

    /*public void filterList(String text) {
        inicioFilterClass.filter(text);
       
    }

    @Override
    public Filter getFilter() {
        return arrayListInicio;
    }*/

private class Filtro extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            filteredContactList.clear();
            final FilterResults results = new FilterResults();
            String text=charSequence.toString();
            if (text != "" || text.length() != 0) {
                for (final InicioItem item : inicioItemFilter) {
                    if (item.nombrePersona().toLowerCase().contains(text)))
filteredContactList.add(item);
                }

                results.values = filteredContactList;
                results.count = filteredContactList.size();
            }
            else
            {
                results.values = inicioItemFilter;
                results.count = inicioItemFilter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

                inicioItemFilter = (ArrayList<InicioItem>) filterResults.values;
                notifyDataSetChanged();
        }
    }