如何检索android的firebase数据

时间:2016-11-20 10:58:40

标签: java android listview firebase firebase-realtime-database

我使用Firebase开发了我的应用。现在我的查询是我从Firebase中找到数据,但没有在我的对象上设置它我的Firebase数据strucre看起来像下面的图像

enter image description here

我的代码是

mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            adapter=new LiveNewsAdapter(dataSnapshot);
            newsList.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

我的适配器类是

public class LiveNewsAdapter extends RecyclerView.Adapter<LiveNewsAdapter.ViewHolder> {
private DataSnapshot mDataset;


public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextView;

   public ViewHolder(View v) {
        super(v);
       mTextView=(TextView) v.findViewById(R.id.tvNewsView);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public LiveNewsAdapter(DataSnapshot myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public LiveNewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.text_livenews, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    System.out.println("DARA  "+ mDataset.getValue());




    for (DataSnapshot messageSnapshot: mDataset.getChildren()) {
        String name = (String) messageSnapshot.child("Divya Bhaskar").getValue();
        String message = (String) messageSnapshot.child("Sandesh").getValue();

        System.out.println("NMAE  : "+name);
    }

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return (int) mDataset.getChildrenCount();
}

}

请帮助我了解如何在Datasnapshot

中设置TextView

0 个答案:

没有答案