Go to another activity by clicking on RecyclerView

1

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

    
asked by kevvelas 08.12.2017 в 03:12
source

2 answers

0

One way to do this is to add a onClickListener to the view of SearchViewHolder :

@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
    holder.nombres.setText(nombresList.get(position));
    holder.nombreusuario.setText(nombreusuarioList.get(position));
    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openActivity();
        }
    });
}

public void openActivity() {
    // ... agrega aqui el codigo para abrir la activity
}

Comment to part: I would recommend creating a POJO user and that the adapter uses a single list of objects of type User. Otherwise it is more complicated to treat each array list separately. Also, if that User is serializable or parcelable, it is easier to pass it as an argument to the activity in a bundle, instead of using each attribute separately, which forces you to map them each time. A simple way to do this is to use getValue(Usuario.class) instead of manually obtaining each attribute. More on this in the documentation: link

    
answered by 08.12.2017 в 14:18
-1

Must be assigning the listener OnClickListener to holder.itemView and within adding the launch of a new Activity using a Intent :

    @Override
    public void onBindViewHolder(SearchViewHolder holder, int position) {
        holder.nombres.setText(nombresList.get(position));
        holder.nombreusuario.setText(nombreusuarioList.get(position));

        //* Agrega listener.
        holder.itemView.setOnClickListener(new View.OnClickListener() {
          @Override
             public void onClick(View view) {

               //* Inicia otra Activity.

               Intent intent = new Intent(SearchActivity.this, OtraActivity.class);
               startActivity(intent);

             }
         });

Glide.with(context).load(fotoperfilList.get(position)).asBitmap().placeholder(R.mipmap.ic_launcher_round).into(holder.foto_perfil);
    }
    
answered by 08.12.2017 в 22:42