mercredi 3 janvier 2018

How do i get the name and email address of the selected checkbox in recyclerview?

This class adds all the users from the firebase database to the recyclerview.I want to add name and email addresses of the selected checkboxes in the recyclerview and then add them to a list

package com.tml.sharethem.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;

public class AddMembers extends AppCompatActivity {


    TextView txtGroup;
    private EditText mSearchField;
    private ImageButton mSearchBtn;
    private RecyclerView mResultList;
    private DatabaseReference mUserDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_members);
        String data = getIntent().getExtras().getString("group_name1");

        txtGroup= (TextView) findViewById(R.id.txtGroup);
        txtGroup.setText("Select Members for "+data+"Group");

        mSearchField= (EditText) findViewById(R.id.search_field);
        mSearchBtn= (ImageButton) findViewById(R.id.search_btn);
        mResultList= (RecyclerView) findViewById(R.id.result__list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mUserDatabase= FirebaseDatabase.getInstance().getReference("Users");
            FirebaseRecyclerAdapter<Users,UsersViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Users, UsersViewHolder>(Users.class
                    ,R.layout.list_layout,
                    UsersViewHolder.class,
                    mUserDatabase) {
                @Override
                protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {


                    viewHolder.setDetails(model.getName(),model.getEmail());

                }


            };
            mResultList.setAdapter(firebaseRecyclerAdapter);
        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String searchText=mSearchField.getText().toString();
                firebaseUserSearch(searchText);
            }
        });
    }

    private void firebaseUserSearch(String searchText) {
        Toast.makeText(AddMembers.this,"Search Started",Toast.LENGTH_LONG).show();
        Query firebaseSearchQuery=mUserDatabase.orderByChild("name").startAt(searchText).endAt(searchText+"\uf8ff");
        FirebaseRecyclerAdapter<Users,UsersViewHolder> firebaseRecyclerAdapter1=new FirebaseRecyclerAdapter<Users, UsersViewHolder>(Users.class
                ,R.layout.list_layout,
                UsersViewHolder.class,
                firebaseSearchQuery) {
            @Override
            protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {


                viewHolder.setDetails(model.getName(),model.getEmail());

            }
        };
        mResultList.setAdapter(firebaseRecyclerAdapter1);
    }

    public static class UsersViewHolder extends RecyclerView.ViewHolder {

        View mView;
        public UsersViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
        }

        TextView user_name;
        TextView user_email;
        CheckBox checkBox;
        public void setDetails(String userName,String userEmail)
        {

            user_name=(TextView)mView.findViewById(R.id.name_text);
            user_email=(TextView)mView.findViewById(R.id.email_text);
            checkBox= (CheckBox) mView.findViewById(R.id.checkBox);


            user_name.setText(userName);
            user_email.setText(userEmail);
            boolean checked=checkBox.isChecked();


        }

    }

}

This is the model class of the recyclerview Users.java

package com.tml.sharethem.demo;

/**
 * Created by SONI on 01/01/2018.
 */

public class Users {
    String name,email;
    private boolean isSelected;
    public Users(){

    }
    public Users(String name, String email) {
        this.name = name;
        this.email = email;
    }
    public Users(String name, String email, boolean isSelected) {

        this.name = name;
        this.email = email;
        this.isSelected = isSelected;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

}

This is the xml code for the main recyclerview. addmembers.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="10dp"
    tools:context="com.tml.sharethem.demo.AddMembers">

    <TextView
        android:id="@+id/txtGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="25dp"
        android:text="" />

    <EditText
        android:id="@+id/search_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/txtGroup"
        android:layout_marginTop="27dp"
        android:background="@drawable/search_layout"
        android:ems="10"
        android:inputType="textPersonName"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:hint="Search Here..."/>

    <ImageButton
        android:id="@+id/search_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/search_field"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:layout_toEndOf="@+id/search_field"
        android:layout_toRightOf="@+id/search_field"
        app:srcCompat="@android:drawable/ic_menu_search" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/result__list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_below="@+id/search_btn"
        android:layout_marginTop="10dp">

    </android.support.v7.widget.RecyclerView>

    <Button
        android:id="@+id/btn_add_members"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Add Selected Members" />

</RelativeLayout>

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginLeft="70dp"

        android:layout_marginTop="20dp"

        android:text="TextView" />

    <TextView
        android:id="@+id/email_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name_text"
        android:layout_below="@+id/name_text"
        android:layout_marginTop="10dp"
        android:text="TextView" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/name_text"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:text="" />

</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire