我想将Firebase数据库中的数据读取到RecyclerView中,但是当我运行该程序并打开应该显示数据的活动时,它什么也没有显示,并且在Android Studio中也没有出现错误。 / p>
我尝试了Internet上的所有解决方案,但没有任何效果
这是我的适配器
package com.example.rsolveapp;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class AdapterDescRecyclerView extends RecyclerView.Adapter<AdapterDescRecyclerView.ViewHolder> {
private ArrayList<Description> daftarBarang;
private Context context;
public AdapterDescRecyclerView(ArrayList<Description> barangs, Context ctx){
daftarBarang = barangs;
context = ctx;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvTitle;
ViewHolder(View v) {
super(v);
tvTitle = (TextView) v.findViewById(R.id.tv_namabarang);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_job, parent, false);
// mengeset ukuran view, margin, padding, dan parameter layout lainnya
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final String name = daftarBarang.get(position).getDescription();
holder.tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
holder.tvTitle.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
holder.tvTitle.setText(name);
}
@Override
public int getItemCount() {
return daftarBarang.size();
}
}
这是我显示数据的活动
package com.example.rsolveapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
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 java.util.ArrayList;
public class RsolverMatch extends AppCompatActivity{
private DatabaseReference database;
private RecyclerView rvView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Description> daftarBarang;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rsolvermatch);
rvView = (RecyclerView) findViewById(R.id.rv_main);
rvView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
rvView.setLayoutManager(layoutManager);
database = FirebaseDatabase.getInstance().getReference();
database.child("description").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
daftarBarang = new ArrayList<>();
for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren()) {
Description description = noteDataSnapshot.getValue(Description.class);
description.setKey(noteDataSnapshot.getKey());
daftarBarang.add(description);
}
adapter = new AdapterDescRecyclerView(daftarBarang, RsolverMatch.this);
rvView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println(databaseError.getDetails()+" "+databaseError.getMessage());
}
});
}
public static Intent getActIntent(Activity activity){
return new Intent(activity, RsolverMatch.class);
}
}
这是Firebase数据库的屏幕截图
Screenshot of Firebase Database
这是我的Desciption.java(类)
package com.example.rsolveapp;
import com.google.firebase.database.IgnoreExtraProperties;
import java.io.Serializable;
public class Description implements Serializable{
private String description;
private String day, sHour, sMin, eHour, eMin;
private String key;
public Description(){
}
public String getKey(){
return key;
}
public void setKey(String key){
this.key=key;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description=description;
}
public String getDay(){
return day;
}
public void setDay(String day){
this.day=day;
}
public String getsHour(){
return sHour;
}
public void setsHour(String sHour){
this.sHour=sHour;
}
public String getsMin(){
return sMin;
}
public void setsMin(String sMin){
this.sMin=sMin;
}
public String geteHour(){
return eHour;
}
public void seteHour(String eHour){
this.eHour=eHour;
}
public String geteMin(){
return eMin;
}
public void seteMin(String eMin){
this.eMin=eMin;
}
@Override
public String toString(){
return " "+description+"/n"+
" "+day+"/n"+
" "+sHour+"/n"+
" "+sMin+"/n"+
" "+eHour+"/n"+
" "+eMin;
}
public Description(String desc, String dayz, String startH, String startM, String endH, String endM){
description=desc;
day=dayz;
sHour=startH;
sMin=startM;
eHour=endH;
eMin=endM;
}
}