这是我从cloud firestore检索数据的代码。现在我需要在活动中添加一个搜索视图,这样更容易在活动中找到用户,但我不知道如何做到,也不知道在哪里添加。如果有人能帮我的话。
这是我检索数据的代码。
Users.java:
package com.example.how;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
public class Users extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference UsersRef = db.collection("Users");
private UsersAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = UsersRef;
FirestoreRecyclerOptions<UsersModel> options = new
FirestoreRecyclerOptions.Builder<UsersModel>()
.setQuery(query, UsersModel.class)
.build();
adapter = new UsersAdapter(options);
RecyclerView recyclerView = findViewById(R.id.FireStoreList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
这里是activity_users.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".Users">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/FireStoreList"
android:layout_width="409dp"
android:layout_height="686dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
这里是userdetails.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/rectangle">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rectangle"
android:padding="0dp">
<TextView
android:id="@+id/txtFName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="147dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView11"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtLName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtFName"
android:layout_marginStart="32dp"
android:layout_marginBottom="121dp"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView12" />
<TextView
android:id="@+id/txtDOB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLName"
android:layout_marginStart="22dp"
android:layout_marginBottom="94dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView13" />
<TextView
android:id="@+id/txtemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtDOB"
android:layout_marginStart="65dp"
android:layout_marginBottom="63dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView14" />
<TextView
android:id="@+id/txtPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtemail"
android:layout_marginStart="10dp"
android:layout_marginBottom="33dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView15" />
<TextView
android:id="@+id/txtUserID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPhoneNumber"
android:layout_marginStart="55dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView16" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:text="First Name: "
app:layout_constraintBottom_toTopOf="@+id/textView12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="8dp"
android:text="Last Name: "
app:layout_constraintBottom_toTopOf="@+id/textView13"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="11dp"
android:text="Date of Birth: "
app:layout_constraintBottom_toTopOf="@+id/textView14"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="11dp"
android:text="Email: "
app:layout_constraintBottom_toTopOf="@+id/textView15"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="12dp"
android:text="Phone Number:"
app:layout_constraintBottom_toTopOf="@+id/textView16"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="User ID: "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
这是一个简单的解决方案,麻烦最小:只需使用提交按钮将单行EditText添加到活动布局中,然后只需启动另一个单击按钮的"活动"操作即可显示搜索结果。在xml布局中添加元素,然后在Java代码中连接它们。