如何使用RecyclerView从Firebase检索数据

时间:2018-07-12 10:45:18

标签: firebase android-recyclerview model adapter

我在Firebase中检索数据时遇到问题,输出未出现,logcat显示没有这样的适配器,“ RecyclerView:未连接适配器;正在跳过布局”。

ListWord.java

public class ListWord extends AppCompatActivity {

    View Word;

    RecyclerView wordList;
    LinearLayoutManager layoutManager;
    FirebaseRecyclerAdapter<WordDictionary,CategoryWordViewHolder> adapter;

    FirebaseDatabase database;
    DatabaseReference word;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_word);

        database = FirebaseDatabase.getInstance();
        word = database.getReference("Category_Learn");
    }


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

        wordList = (RecyclerView)Word.findViewById(R.id.listWord);
        wordList.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(container.getContext());
        wordList.setLayoutManager(layoutManager);

        loadWord();

        return Word;
    }

    private void loadWord() {

        adapter = new FirebaseRecyclerAdapter<WordDictionary,CategoryWordViewHolder>(WordDictionary.class, R.layout.word_item, CategoryWordViewHolder.class,word) {
            @Override
            protected void populateViewHolder(CategoryWordViewHolder viewHolder, final WordDictionary model, final int position) {

                viewHolder.word.setText(model.getWord());
                viewHolder.meaning.setText(model.getDescription());
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        //Toast.makeText(getActivity(),String.format("%s|%s",adapter.getRef(position).getKey(),model.getName()), Toast.LENGTH_SHORT).show();
                        Intent startGame = new Intent(getApplicationContext(),ListWord.class);
                        Common.categoryId = adapter.getRef(position).getKey();
                        Common.word = model.getWord();
                        Common.meaning = model.getDescription();
                        startActivity(startGame);
                    }
                });

            }
        };
        adapter.notifyDataSetChanged();
        wordList.setAdapter(adapter);
    }
    }

WordDictionary.java模型

public class WordDictionary {

    private String categoryId;
    private String categoryName;
    private String word;
    private String description;

    public WordDictionary(String id, String categoryId, String categoryName, String word, String description) {
        this.categoryId = categoryId;
        this.categoryName = categoryName;
        this.word = word;
        this.description = description;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

CategoryWordViewHolder.java

public class CategoryWordViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView category_id;
    public TextView category_name;
    public TextView word;
    public TextView meaning;

    public ItemClickListener itemClickListener;

    public CategoryWordViewHolder(View itemView) {
        super(itemView);

        category_name = (TextView)itemView.findViewById(R.id.category_name);
        word = (TextView)itemView.findViewById(R.id.txt_word);
        meaning = (TextView)itemView.findViewById(R.id.txt_meaning);

        itemView.setOnClickListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view,getAdapterPosition(),false);

    }
}

0 个答案:

没有答案