gpt4 book ai didi

android - Firebase 回收器适配器不适用于 child 的回收器 View

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:15 25 4
gpt4 key购买 nike

我有一个数据库,我的数据如下所示。

enter image description here

我已将 Firebase Recycler Adapter 用于 S1、S2 等的信息,以在回收器 View 中表示该数据。但是,数据不仅仅显示在我的应用程序中。该应用程序供教师通过从 Firebase 检索数据来查看学生是否在场。

AttendaceFrag.java

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
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;



public class AttendanceFrag extends Fragment {

RecyclerView recyclerView;
DatabaseReference databaseReference;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_attendance_frag, container, false);

recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));


databaseReference = FirebaseDatabase.getInstance().getReference().child("Student");


databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
Toast.makeText(getContext(), "Data to show", Toast.LENGTH_LONG).show();
startShowing();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

return v;
}

private void startShowing() {
FirebaseRecyclerAdapter<Student, AttendanceFrag.RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Student, AttendanceFrag.RequestViewHolder>(
Student.class,
R.layout.custom_row,
AttendanceFrag.RequestViewHolder.class,
databaseReference
) {
@Override
protected void populateViewHolder(AttendanceFrag.RequestViewHolder viewHolder, Student model, int position) {

viewHolder.setName(model.getName());
viewHolder.setAp(model.getAp());
viewHolder.setRegno(model.getRegno());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}

public static class RequestViewHolder extends RecyclerView.ViewHolder {
View mView;

public RequestViewHolder(View itemView) {
super(itemView);
mView = itemView;
}

public void setName(String name) {
TextView stname = (TextView) mView.findViewById(R.id.st_name);
stname.setText(name);
}

public void setRegno(String regno) {
TextView stregno = (TextView) mView.findViewById(R.id.st_regno);
stregno.setText(regno);
}

public void setAp(String ap) {
TextView stap = (TextView) mView.findViewById(R.id.st_ap);
stap.setText(ap);
}
}
}

学生.java

public class Student {

private String name, regno,ap;

public Student(String name, String regno, String ap) {
this.name = name;
this.regno = regno;
this.ap = ap;
}

public Student() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getRegno() {
return regno;
}

public void setRegno(String regno) {
this.regno = regno;
}

public String getAp() {
return ap;
}

public void setAp(String ap) {
this.ap = ap;
}
}

activity_attendance_frag.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:padding="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xxx.AttendanceFrag">

<TableLayout
android:id="@+id/table_lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TableRow
android:id="@+id/firstrow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:background="@drawable/r1"
android:text="Sr. No."
android:id="@+id/srnohead"
android:textAlignment="center"
android:padding="5dp"
android:textColor="@color/colorPrimaryDark"
/>
<TextView
android:padding="5dp"
android:textAlignment="center"
android:background="@drawable/r1"
android:text="Reg. Number"
android:layout_width="135dp"
android:id="@+id/regNohead"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:padding="5dp"
android:background="@drawable/r1"
android:text="Name"
android:textAlignment="center"
android:layout_height="wrap_content"
android:layout_width="120dp"
android:id="@+id/namehead"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:padding="5dp"
android:background="@drawable/r1"
android:text="P/A"
android:layout_height="wrap_content"
android:id="@+id/attnhead"
android:textColor="@color/colorPrimaryDark"/>
</TableRow>
</TableLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/table_lay"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp" />

</RelativeLayout>

自定义行.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:padding="3dp">

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/r1"
android:text="Sr. No."
android:id="@+id/st_sr"
android:textAlignment="center"
android:padding="5dp"
android:textColor="@color/colorPrimaryDark"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="135dp"
android:padding="5dp"
android:textAlignment="center"
android:background="@drawable/r1"
android:text="Reg. Number"
android:id="@+id/st_regno"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:padding="5dp"
android:background="@drawable/r1"
android:text="Name"
android:textAlignment="center"
android:layout_height="wrap_content"
android:layout_width="120dp"
android:id="@+id/st_name"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:layout_width="wrap_content"
android:padding="5dp"
android:background="@drawable/r1"
android:text="P/A"
android:layout_height="wrap_content"
android:id="@+id/st_ap"
android:textColor="@color/colorPrimaryDark"/>
</LinearLayout>

最佳答案

将其放入您的应用级 gradle 中:实现 'com.firebaseui:firebase-ui-database:3.2.1'

在你的 onStart 方法中输入:

@Override
protected void onStart() {
super.onStart();

Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Blog")
.limitToLast(50);
FirebaseRecyclerOptions<BlogModel> options =
new FirebaseRecyclerOptions.Builder<BlogModel>()
.setQuery(query, BlogModel.class)
.build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<BlogModel, BlogViewHolder>(options) {
@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_blog, parent, false);

return new BlogViewHolder(view);
}

@Override
protected void onBindViewHolder(BlogViewHolder holder, int position, BlogModel model) {
holder.setTitleBlog(model.getTitle());
holder.setTitleDesc(model.getDescription());
holder.setImage(getApplicationContext(), model.getImage_url());
}
};
adapter.startListening();
mRecycler.setAdapter(adapter);

}

在你的 BlogViewHolder 中放这个:

 public static class BlogViewHolder extends RecyclerView.ViewHolder {
View view;

public BlogViewHolder(View itemView) {
super(itemView);
view = itemView;
}

public void setTitleBlog(String title) {
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
tvTitle.setText(title);

}

public void setTitleDesc(String desc) {
TextView tvDesc = (TextView) view.findViewById(R.id.tv_desc);
tvDesc.setText(desc);
}


public void setImage(Context context, String url) {
ImageButton ib = (ImageButton) view.findViewById(R.id.ib_blog);
Picasso.with(context).load(url).into(ib);

}
}

一切准备就绪 :)

关于android - Firebase 回收器适配器不适用于 child 的回收器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43643154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com