此类将firebase数据库中的所有用户添加到recyclerview。我想在recyclerview中添加所选复选框的名称和电子邮件地址,然后将它们添加到列表中
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();
}
}
}
这是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;
}
}
这是主Recyclerview的xml代码。 addmembers.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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>
答案 0 :(得分:0)
实施onCheckChangedListener
一种做法是
在
中执行以下修改 viewHolder.setDetails(model.getName(),model.getEmail());
更改为
viewHolder.setDetails(Users model);
在setDetails()
中进行以下修改checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked) {
//Store in hashmap
hashmap.put(Users.userId,model)
}else {
if(hashMap.containsKey(model.userId)) {
hashMap.remove(model.userId)
}
}
}
答案 1 :(得分:0)
You can use the ArrayList to store id of that particular record, for example, contact id, then on the click of any control you can retrieve the ids from ArrayList.
public static List<Integer> SelectedCheckbox = new ArrayList<Integer>();
在回收站视图内,在复选框上单击添加或删除值 数组列表
public ViewHolder(View itemView) {
myCheckBoxContactItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// set tag inside onBindViewHolder method
int contactId = (int) mConvertView.getTag(R.string.contact_id);
if (((CheckBox) v).isChecked() && !SelectedContact.contains(contactId)) {
SelectedContact.add(contactId);
} else if (CommonUtils.SelectedContact.contains(contactId)) {
int indexOf = CommonUtils.SelectedContact.indexOf(contactId);
SelectedContact.remove(indexOf);
}
}
}
myButton.setOnClickListener(new View.OnClickListener() {
for(int i = 0; i< SelectedCheckbox.size; i++)
{
int contactId = SelectedCheckbox.get(i);
// your database query to get record of particular id.
}
}