我正在尝试使用Firebase
将RecyclerView
数据库中的数据显示为Firebase RecyclerAdapter
。我对此很陌生,无法弄清楚我面临的问题。
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import de.hdodenhof.circleimageview.CircleImageView;
public class AllUsersActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView allUsersList;
private DatabaseReference allDatabaseUserreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_users);
mToolbar = (Toolbar)findViewById(R.id.all_users_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("All Users");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
allUsersList = (RecyclerView)findViewById(R.id.all_users_list);
allUsersList.setHasFixedSize(true);
allUsersList.setLayoutManager(new LinearLayoutManager(this));
allDatabaseUserreference = FirebaseDatabase.getInstance().getReference().child("Users");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<AllUsers> options =
new FirebaseRecyclerOptions.Builder<AllUsers>()
.setQuery(allDatabaseUserreference, AllUsers.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>(options) {
@Override
public AllUsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_users_display_layout, parent, false);
return new AllUsersViewHolder(view);
}
@Override
protected void onBindViewHolder(AllUsersViewHolder viewHolder, int position, AllUsers model) {
viewHolder.setUser_name(model.getUser_name());
viewHolder.setUser_status(model.getUser_status());
viewHolder.setUser_image(getApplicationContext(),model.getUser_image());
}
};
allUsersList.setAdapter(adapter);
}
public static class AllUsersViewHolder extends RecyclerView.ViewHolder{
View mView;
public AllUsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setUser_name(String user_name){
TextView name = (TextView)mView.findViewById(R.id.all_users_username);
name.setText(user_name);
}
public void setUser_status(String user_status){
TextView status = (TextView)mView.findViewById(R.id.all_users_status);
status.setText(user_status);
}
public void setUser_image(Context ctx, String user_image){
CircleImageView image = (CircleImageView) mView.findViewById(R.id.all_users_profile_image);
Picasso.get().load(user_image).into(image);
}
}
}
这是我的布局文件
<?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"
tools:context="com.example.swapnil.chitchat.AllUsersActivity">
<include
layout="@layout/app_bar_layout"
android:id="@+id/all_users_app_bar">
</include>
<android.support.v7.widget.RecyclerView
android:id="@+id/all_users_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/all_users_app_bar">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
应在Recycler视图上生成此类布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="15dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/all_users_profile_image"
android:layout_width="85dp"
android:layout_height="85dp"
android:src="@drawable/defualt_image_profile"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="@+id/all_users_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/all_users_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="User Status"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是我RecyclerAdapter
public class AllUsers {
public String user_name;
public String user_status;
public String user_image;
public AllUsers(){
}
public AllUsers(String user_name, String user_status, String user_image) {
this.user_name = user_name;
this.user_status = user_status;
this.user_image = user_image;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_status() {
return user_status;
}
public void setUser_status(String user_status) {
this.user_status = user_status;
}
public String getUser_image() {
return user_image;
}
public void setUser_image(String user_image) {
this.user_image = user_image;
}
}
当我运行应用程序时,我得到的只是一个空白屏幕。编译或运行代码时没有错误。我尝试通过在onCreateViewHolder
方法中编写Toast消息来调试代码,但它是没有被执行。