java - Android Studio RecyclerView
问题描述
我收到错误 E/RecyclerView:未连接适配器;运行程序时跳过布局。每当我运行代码时,我都无法在搜索片段中看到用户名和全名。我刚刚收到错误 E/RecyclerView: No adapter attach; 在 Logcat 中跳过布局。仅显示图像。我该如何解决这个问题?
我的片段是:
package com.example.getalong.Fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.getalong.Adapter.UserAdapter;
import com.example.getalong.Model.User;
import com.example.getalong.R;
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.ValueEventListener;
import com.hendraanggrian.appcompat.widget.SocialAutoCompleteTextView;
import java.util.ArrayList;
import java.util.List;
public class SearchFragment extends Fragment {
private RecyclerView recyclerView;
private SocialAutoCompleteTextView search_bar;
private List<User> mUsers;
private UserAdapter userAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
recyclerView = view.findViewById(R.id.recycler_view_users);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mUsers = new ArrayList<>();
userAdapter = new UserAdapter(getContext(), mUsers, true);
recyclerView.setAdapter(userAdapter);
search_bar = view.findViewById(R.id.search_bar);
readUsers();
return view;
}
private void readUsers() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(TextUtils.isEmpty(search_bar.getText().toString())){
mUsers.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
User user = snapshot.getValue(User.class);
mUsers.add(user);
}
userAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
我的适配器类是:
package com.example.getalong.Adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.getalong.Model.User;
import com.example.getalong.R;
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.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
private boolean isFragment;
private FirebaseUser firebaseUser;
public UserAdapter(Context mContext, List<User> mUsers, boolean isFragment){
this.mContext = mContext;
this.mUsers = mUsers;
this.isFragment = isFragment;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.user_item, parent, false);
return new UserAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
User user = mUsers.get(position);
holder.btnFollow.setVisibility(View.VISIBLE);
holder.username.setText(user.getUsername());
holder.fullname.setText(user.getName());
Picasso.get().load(user.getImageurl()).placeholder(R.mipmap.ic_launcher).into(holder.imageProfile);
isFollowed(user.getId(), holder.btnFollow);
if(user.getId().equals(firebaseUser.getUid())){
holder.btnFollow.setVisibility(View.GONE);
}
}
private void isFollowed(final String id, final Button btnFollow) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("follow").child(firebaseUser.getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child(id).exists()) {
btnFollow.setText("Following");
}
else {
btnFollow.setText("Follow");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public int getItemCount() {
return mUsers.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public CircleImageView imageProfile;
public TextView username;
public TextView fullname;
public Button btnFollow;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageProfile = itemView.findViewById(R.id.image_profile);
username = itemView.findViewById(R.id.username);
fullname = itemView.findViewById(R.id.fullname);
btnFollow = itemView.findViewById(R.id.btn_follow);
}
}
}
我的模型用户类是:
package com.example.getalong.Model;
public class User {
private String name;
private String email;
private String username;
private String bio;
private String imageurl;
private String id;
public User(){
}
public User(String name, String email, String username, String bio, String imageurl, String id){
this.name = name;
this.email = email;
this.username = username;
this.imageurl = imageurl;
this.bio = bio;
this.id = id;
}
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 String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我的资源文件是:
片段搜索.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.SearchFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bar"
android:background="?android:attr/windowBackground">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar"
android:background="?android:attr/windowBackground">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_search"/>
<com.hendraanggrian.appcompat.widget.SocialAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_bar"
android:background="@android:color/transparent"
android:hint="Search"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view_users"
android:layout_below="@id/bar"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view_tags"
android:layout_below="@id/recycler_view_users"
android:layout_marginTop="5dp"/>
</RelativeLayout>
用户项目.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"
android:padding="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/image_profile"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/image_profile"
android:layout_toRightOf="@id/image_profile"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_centerVertical="true">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Username"
android:textColor="@color/design_default_color_primary_dark"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fullname"
android:text="Fullname"
android:maxLines="1"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="@drawable/button_background"
android:backgroundTint="@color/background"
app:backgroundTint="@color/background"
android:id="@+id/btn_follow"
android:textColor="@color/colorPrimary"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone"/>
</RelativeLayout>
解决方案
将您的适配器放在回调方法中。
User user = snapshot.getValue(User.class);
mUsers.add(user);
}
userAdapter = new UserAdapter(getContext(), mUsers, true);
recyclerView.setAdapter(userAdapter)
推荐阅读
- django - 在 django 中使用正确的 pk 值重定向时出现问题
- php - 如何通过ajax设置相同输入字段但在不同行中的值
- postgresql - Spring Boot Hibernate Update 语句需要 7 秒来持久化 10kb 实体
- reactjs - React Admin:我想路由到
从列表中查看 - python - 从python中二维数组的每一行中随机选择N个元素
- javascript - 使用 firebase 函数的共享模块和打字稿别名
- azure - 在非交互式环境中验证和使用 Firebase
- excel - 分配等值随机
- javascript - javascript为游戏添加背景音频
- python-3.x - Kivy 应用程序登录