我正在使用firebaseui的Firebase回收器适配器来显示数据库中的值 这是我从数据库中获取值的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRef = FirebaseDatabase.getInstance().getReference();
listRef =mRef.child("itemlist");
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerViewLayoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(false);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(MainActivity.this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View childView, int position) {
Log.v(TAG,"YouClickon"+ position);
}
}){
}
);
}
@Override
protected void onStart() {
super.onStart();
attachRecyclerViewAdapter();
}
@Override
protected void onStop() {
super.onStop();
if(mRecyclerViewAdapter != null){
mRecyclerViewAdapter.cleanup();
}
}
private void attachRecyclerViewAdapter() {
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<ItemList, ItemHolder>(
ItemList.class,
R.layout.shop_recycler_row_view,
ItemHolder.class,
listRef
) {
@Override
protected void populateViewHolder(ItemHolder itemHolder, ItemList item, int i) {
itemHolder.setName(item.getName());
itemHolder.setDescription(item.getDescription());
}
};
recyclerView.setAdapter(mRecyclerViewAdapter);
}
public static class ItemHolder extends RecyclerView.ViewHolder{
View mView;
public ItemHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name){
TextView field = (TextView) mView.findViewById(R.id.tv_shopName);
field.setText(name);
}
public void setDescription(String text) {
TextView field = (TextView) mView.findViewById(R.id.tv_shopDesc);
field.setText(text);
}
}
数据库的json结构如下:
{
"itemlist" : {
"-KJ5USfIBXgAfpurhHjE" : {
"-KJ5Uf0XkahJ5fqio8kq" : {
"description" : "Minal",
"img_url" : "HIU",
"name" : "Milan ni item"
},
"-KJ5Ul7ZT2oKuCLxOY78" : {
"description" : "nathi kevi😷😷",
"img_url" : "HIU",
"name" : "mari item"
}
},
"-KJ5VJtTUQqKnHmuu1tY" : {
"-KJ5d7aggDnYKz65PMHa" : {
"description" : "whaajakakq",
"img_url" : "HIU",
"name" : "Sweet"
}
}
},
"shoplist" : {
"-KJ5USfIBXgAfpurhHjE" : {
"description" : "Milan ne Maru mane aaje muki ne chalya gaya fir ek baar",
"img_url" : "https://firebasestorage.googleapis.com/v0/b/fooder-5eba6.appspot.com/o/shop_images%2Fimage%3A26208?alt=media&token=75228031-dccd-4c45-bfcf-abe634d58bc6",
"name" : "31st May 2016"
},
"-KJ5Uy4ALcLcJhKH6o9U" : {
"description" : "shop bhavnagar",
"img_url" : "https://firebasestorage.googleapis.com/v0/b/fooder-5eba6.appspot.com/o/shop_images%2Fimage%3A26213?alt=media&token=7aa14525-0fc5-4c11-a8fb-a44ab121bcfa",
"name" : "Daas"
},
"-KJ5VJtTUQqKnHmuu1tY" : {
"description" : "dukaan",
"img_url" : "https://firebasestorage.googleapis.com/v0/b/fooder-5eba6.appspot.com/o/shop_images%2Fimage%3A26203?alt=media&token=748363be-f0ab-45b4-bd7d-f58be2308b6c",
"name" : "offline"
}
}
}
现在我想显示itemlist中的值。我怎么能这样做?。
答案 0 :(得分:0)
我总是从firebase中检索值,如下所示
//GET DATA FROM FIREBASE INTO ARRAYLIST
fb_to_read.addValuesEventListener....{
public void onDataChange(DataSnapshot result){
List<String> lst = new ArrayList<String>(); // Result will be holded Here
for(DataSnapshot dsp : result.getChildren()){
lst.add(String.valueOf(dsp.getKey())); //add result into array list
}
//NOW YOU HAVE ARRAYLIST WHICH HOLD RESULTS
for(String data:lst){
Toast.make(context,data,Toast.LONG_LENGTH).show;
}
}
}