2017-04-26 72 views
1

我有一个数据库,我的数据看起来像这样。Firebase Recycler Adapter不适用于儿童的回收站视图

enter image description here

我已经使用火力地堡回收站适配器为S1,S2等的信息,来表示在再循环器视图的数据。但是,数据不会仅在我的应用程序中显示。该应用程序供老师通过从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); 
     } 
    } 
} 

Student.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> 

custom_row.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> 

回答

0

你的数据库引用指向错误的节点,你可以使用它像这样:

FirebaseDatabase.getInstance().getReferenceFromUrl("https://facapp-bee2.firebaseio.com/"); 

然后.child(“教师/学生”)和recylerview不使用addValueListener。 Here你可以找到如何使用火力

0

把这个在您的应用程序级gradle这个回收的观点:实行“com.firebaseui:火力-UI-数据库: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); 

     } 
    } 

和你都设置去:)

相关问题