Hi, I'm working with a RecyclerView
and a barra de buscar
in Android Studio
with Firebase
, my search bar if it works but what I try to do is that when I click on an option of RecyclerView
is sent to another activity
different to another (depending on the option you chose).
My code is as follows.
This part is the Activity
where everything is displayed (the barra de buscar
, recyclerview
).
public class SearchBarActivity extends AppCompatActivity {
//-------------------
EditText buscar_texto;
RecyclerView recyclerView;
//--
DatabaseReference databaseReference;
FirebaseUser firebaseUser;
//--
ArrayList<String> nombresList;
ArrayList<String> nombreusuarioList;
ArrayList<String> fotoperfilList;
SearchAdapter searchAdapter;
//---------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_bar);
//----------------------------------------------
buscar_texto= (EditText) findViewById(R.id.buscar_texto);
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
//--------------
databaseReference= FirebaseDatabase.getInstance().getReference();
firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
//--------------
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
//--------------
nombresList=new ArrayList<>();
nombreusuarioList=new ArrayList<>();
fotoperfilList=new ArrayList<>();
//-----------------------------------------------
buscar_texto.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
setAdapter(s.toString());
}else {
nombresList.clear();
nombreusuarioList.clear();
fotoperfilList.clear();
recyclerView.removeAllViews();
}
}
});
}
//----------------------------------------------------------------------
private void setAdapter(final String searchedString) {
databaseReference.child("usuarios").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
nombresList.clear();
nombreusuarioList.clear();
fotoperfilList.clear();
recyclerView.removeAllViews();
int counter = 0;
for (DataSnapshot snapshot: dataSnapshot.getChildren()){
String uid = snapshot.getKey();
String nombres = snapshot.child("nombres").getValue(String.class);
String nombreusuario = snapshot.child("nombreusuario").getValue(String.class);
String foto_perfil = snapshot.child("foto_perfil").getValue(String.class);
if (nombres.toLowerCase().contains(searchedString.toLowerCase())){
nombresList.add(nombres);
nombreusuarioList.add(nombreusuario);
fotoperfilList.add(foto_perfil);
counter++;
}else if (nombreusuario.toLowerCase().contains(searchedString.toLowerCase())){
nombresList.add(nombres);
nombreusuarioList.add(nombreusuario);
fotoperfilList.add(foto_perfil);
counter++;
}
if (counter == 15)
break;
}
searchAdapter = new SearchAdapter(SearchBarActivity.this,nombresList,nombreusuarioList,fotoperfilList);
recyclerView.setAdapter(searchAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
This class is the part of the SearchAdapter
shown above
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {
Context context;
ArrayList<String> nombresList;
ArrayList<String> nombreusuarioList;
ArrayList<String> fotoperfilList;
class SearchViewHolder extends RecyclerView.ViewHolder{
ImageView foto_perfil;
TextView nombres,nombreusuario;
public SearchViewHolder(View itemView) {
super(itemView);
foto_perfil = itemView.findViewById(R.id.fotoperfil);
nombres = itemView.findViewById(R.id.nombres);
nombreusuario=itemView.findViewById(R.id.nombreusuario);
}
}
public SearchAdapter(Context context, ArrayList<String> nombresList, ArrayList<String> nombreusuarioList, ArrayList<String> fotoperfilList) {
this.context = context;
this.nombresList = nombresList;
this.nombreusuarioList = nombreusuarioList;
this.fotoperfilList = fotoperfilList;
}
@Override
public SearchAdapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.search_list_items,parent, false);
return new SearchAdapter.SearchViewHolder(view);
}
@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
holder.nombres.setText(nombresList.get(position));
holder.nombreusuario.setText(nombreusuarioList.get(position));
Glide.with(context).load(fotoperfilList.get(position)).asBitmap().placeholder(R.mipmap.ic_launcher_round).into(holder.foto_perfil);
}
@Override
public int getItemCount() {
return nombresList.size();
}
}
activity_search_bar
search_list_items
my database in firebase