RecyclerView没有显示来自Firestore的任何数据

时间:2018-08-04 21:12:30

标签: java android android-recyclerview

我在firestore数据库中有主题类别列表,并且希望将其显示在我的应用程序中(我认为可以通过recyclerView完成)。但是我的RecyclerView没有填充任何数据。您可以在下面看到我的代码:

Question_list.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

public class Question_list extends AppCompatActivity {

private static final String TAG ="Firelog" ;
private RecyclerView mMainList;
private FirebaseFirestore mFirestore;
private CategoryAdapter categoryAdapter;
private List<Categories> list_category;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question_list);

    mMainList = (RecyclerView)findViewById(R.id.main_list_id);
    //mMainList.setHasFixedSize(true);
    mMainList.setLayoutManager(new LinearLayoutManager(this));
    mMainList.setAdapter(categoryAdapter);
    mFirestore = FirebaseFirestore.getInstance();
    categoryAdapter = new CategoryAdapter(list_category);
    list_category = new ArrayList<>();

    mFirestore.collection("Questions").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

            if(e!=null){
                Log.d(TAG, "Error: "+ e.getMessage());
            }
            for(DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){
                if(doc.getType() == DocumentChange.Type.MODIFIED){
                    //String document = doc.getDocument().getString("Category");

                    Categories categories = doc.getDocument().toObject(Categories.class);
                    list_category.add(categories);
                    Log.d(TAG, "Category: "+ categories);
                    categoryAdapter.notifyDataSetChanged();
                }
            }
        }
    });

}
}

CategoryAdapter.java

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;

public class CategoryAdapter extends 
RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

public List<Categories> list_categorie;
public CategoryAdapter(List<Categories> list_categorie){
    this.list_categorie = list_categorie;

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_list,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    holder.mCategoryText.setText(list_categorie.get(position).getCategory());

}

@Override
public int getItemCount() {
    return list_categorie.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    View mview;
    public TextView  mCategoryText;
    public ViewHolder(View itemView) {
        super(itemView);
        mview = itemView;
        mCategoryText = (TextView)mview.findViewById(R.id.category_text_id);

    }
}
}

Categories.java

public class Categories {


String category;

public Categories(){}

public Categories(String category) {
    this.category = category;
}

public String getCategory() {
    return category;
}

}

以上是我所有的Java类。我还没有上传XML文件,因为我认为问题出在Java类中。

1 个答案:

答案 0 :(得分:0)

categoryAdapter = new CategoryAdapter(list_category);
list_category = new ArrayList<>();

list_category应该先初始化,然后再传递给适配器

list_category = new ArrayList<>();
categoryAdapter = new CategoryAdapter(list_category);