我没有幸运地看到堆栈溢出上的类似问题。我正在尝试构建一个允许搜索功能进入Firebase实时数据库的活动。
我正在尝试使用FirebaseRecyclerAdapter
来填充一个RecyclerView,其中只包含' / users'中孩子的名字。在搜索小部件上单击“输入”按钮时的节点。
我能够检索数据快照,但无法让适配器填充RecyclerView。
这是我的活动: -
package calypsodating.android.com.calypso;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import calypsodating.android.com.calypso.models.User;
public class AddFriendsActivity extends AppCompatActivity {
private static final String TAG = "AddFriendsActivity";
private SearchManager mSearchManager;
private SearchView mSearchView;
private RecyclerView mRecyclerView;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: starts");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_friends);
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: starts");
Log.d(TAG, "onDataChange: " + dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mSearchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView = findViewById(R.id.searchView);
mSearchView.setSearchableInfo(mSearchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(false);
mRecyclerView = findViewById(R.id.recyclerView_resultList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//Get the intent, verify the action and get the query
Intent intent = getIntent();
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String query = intent.getStringExtra(SearchManager.QUERY);
searchFriends(query);
}
}
private void searchFriends(String q){
Log.d(TAG, "searchFriends: starts");
Query userQuery = mDatabase.orderByKey();
Log.d(TAG, "searchFriends: userQuery = " + userQuery);
FirebaseRecyclerOptions<User> options =
new FirebaseRecyclerOptions.Builder<User>()
.setQuery(userQuery, User.class)
.build();
FirebaseRecyclerAdapter<User, UserViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UserViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull User model) {
Log.d(TAG, "onBindViewHolder: starts");
holder.setDetails(model.getFirstName());
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: starts");
return null;
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
Log.d(TAG, "searchFriends: recyclerview called");
}
// View Holder Class
public class UserViewHolder extends RecyclerView.ViewHolder{
View mView;
public UserViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails( String userName){
Log.d(TAG, "setDetails: starts");
TextView user_name = mView.findViewById(R.id.textView_searchResultName);
user_name.setText(userName);
}
}
}
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="calypsodating.android.com.calypso.AddFriendsActivity">
<SearchView
android:id="@+id/searchView"
android:layout_width="368dp"
android:layout_height="44dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_resultList"
android:layout_width="0dp"
android:layout_height="389dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"></android.support.v7.widget.RecyclerView>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView"/>
</android.support.constraint.ConstraintLayout>
以下是应该填充回收站的相关列表布局:
<?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="match_parent">
<ImageView
android:id="@+id/imageView_searchResultPhoto"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/com_facebook_profile_picture_blank_square"/>
<TextView
android:id="@+id/textView_searchResultName"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView_searchResultPhoto"
android:layout_toRightOf="@+id/imageView_searchResultPhoto"
android:text="TextView"
android:textSize="30sp"/>
</RelativeLayout>
这是logcat,您可以看到成功检索到数据快照:
02-07 19:59:53.297 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: onCreate: starts
02-07 19:59:53.300 7037-7037/calypsodating.android.com.calypso V/FA: onActivityCreated
02-07 19:59:53.468 7037-7055/calypsodating.android.com.calypso V/FA: Activity resumed, time: 25910369
02-07 19:59:53.472 7037-7055/calypsodating.android.com.calypso D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=ProfileCreationPageOne, firebase_previous_id(_pi)=-1873360491649842747, firebase_screen_class(_sc)=AddFriendsActivity, firebase_screen_id(_si)=-1873360491649842746}]
02-07 19:59:53.552 7037-7037/calypsodating.android.com.calypso E/RecyclerView: No adapter attached; skipping layout
02-07 19:59:53.558 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: onDataChange: starts
02-07 19:59:53.558 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: onDataChange: DataSnapshot { key = users, value = {5={lastName=Tarney, firstName=Nicole}, 4={lastName=Booker, firstName=Hailey}, 1={lastName=Smith, firstName=Kelly}, P18G8q2AHfV8fI13cS0TpnnkFqa2={lastName=Ak, firstName=Osman, gender=Male, age=25}, 3={lastName=Williams, firstName=Brittney}, 2={lastName=Johnson, firstName=Sara}} }
02-07 19:59:53.581 7037-7077/calypsodating.android.com.calypso D/EGL_emulation: eglMakeCurrent: 0xa82058a0: ver 2 0 (tinfo 0xa8203400)
02-07 19:59:53.633 7037-7077/calypsodating.android.com.calypso D/EGL_emulation: eglMakeCurrent: 0xa82058a0: ver 2 0 (tinfo 0xa8203400)
02-07 19:59:53.664 7037-7077/calypsodating.android.com.calypso D/EGL_emulation: eglMakeCurrent: 0xa82058a0: ver 2 0 (tinfo 0xa8203400)
02-07 19:59:53.697 7037-7077/calypsodating.android.com.calypso D/EGL_emulation: eglMakeCurrent: 0xa82058a0: ver 2 0 (tinfo 0xa8203400)
02-07 19:59:53.747 7037-7037/calypsodating.android.com.calypso W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
02-07 19:59:53.749 7037-7077/calypsodating.android.com.calypso D/EGL_emulation: eglMakeCurrent: 0xa82058a0: ver 2 0 (tinfo 0xa8203400)
02-07 19:59:53.795 7037-7077/calypsodating.android.com.calypso D/OpenGLRenderer: endAllActiveAnimators on 0xa83ee000 (RippleDrawable) with handle 0x840b6ba0
02-07 19:59:53.814 7037-7037/calypsodating.android.com.calypso E/RecyclerView: No adapter attached; skipping layout
02-07 19:59:54.154 7037-7037/calypsodating.android.com.calypso E/RecyclerView: No adapter attached; skipping layout
02-07 19:59:57.165 7037-7037/calypsodating.android.com.calypso E/RecyclerView: No adapter attached; skipping layout
02-07 19:59:58.190 7037-7055/calypsodating.android.com.calypso V/FA: Recording user engagement, ms: 4724
02-07 19:59:58.201 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: onCreate: starts
02-07 19:59:58.202 7037-7037/calypsodating.android.com.calypso V/FA: onActivityCreated
02-07 19:59:58.215 7037-7055/calypsodating.android.com.calypso V/FA: Activity paused, time: 25915081
02-07 19:59:58.238 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: searchFriends: starts
02-07 19:59:58.240 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: searchFriends: userQuery = com.google.firebase.database.Query@7a6036d
02-07 19:59:58.250 7037-7055/calypsodating.android.com.calypso D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=4724, firebase_screen_class(_sc)=AddFriendsActivity, firebase_screen_id(_si)=-1873360491649842746}]
02-07 19:59:58.259 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: searchFriends: recyclerview called
02-07 19:59:58.274 7037-7042/calypsodating.android.com.calypso I/art: Do partial code cache collection, code=27KB, data=31KB
02-07 19:59:58.275 7037-7042/calypsodating.android.com.calypso I/art: After code cache collection, code=27KB, data=31KB
02-07 19:59:58.275 7037-7042/calypsodating.android.com.calypso I/art: Increasing code cache capacity to 128KB
02-07 19:59:58.316 7037-7055/calypsodating.android.com.calypso V/FA: Activity resumed, time: 25915167
02-07 19:59:58.318 7037-7055/calypsodating.android.com.calypso D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=AddFriendsActivity, firebase_previous_id(_pi)=-1873360491649842746, firebase_screen_class(_sc)=AddFriendsActivity, firebase_screen_id(_si)=-1873360491649842745}]
02-07 19:59:58.340 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: onDataChange: starts
02-07 19:59:58.340 7037-7037/calypsodating.android.com.calypso D/AddFriendsActivity: onDataChange: DataSnapshot { key = users, value = {5={lastName=Tarney, firstName=Nicole}, 4={lastName=Booker, firstName=Hailey}, 1={lastName=Smith, firstName=Kelly}, 3={lastName=Williams, firstName=Brittney}, 2={lastName=Johnson, firstName=Sara}} }
02-07 19:59:58.350 7037-7077/calypsodating.android.com.calypso D/EGL_emulation: eglMakeCurrent: 0xa82058a0: ver 2 0 (tinfo 0xa8203400)
02-07 19:59:58.530 7037-7037/calypsodating.android.com.calypso W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
02-07 20:00:03.419 7037-7055/calypsodating.android.com.calypso V/FA: Inactivity, disconnecting from the service
02-07 20:04:51.387 7037-7052/calypsodating.android.com.calypso I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
02-07 20:04:51.387 7037-7052/calypsodating.android.com.calypso I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
对于看似简单的答案,我感到很难过。我已经在线查看,但大多数答案都包含过时的firebaseUI版本,这些版本使用的方法我甚至不再在文档中看到(例如:populateViewHolder()
)。任何帮助将不胜感激。
答案 0 :(得分:2)
添加以下行:
go get
在此之下:
firebaseRecyclerAdapter.startListening();
在searchFriends()方法中。