2016-11-09 24 views
1

FIREBASE DATABASE我能夠找回火力地堡數據,但不能添加到ArrayList的

我能夠從火力地堡中讀取數據,但是當我嘗試添加值(項目)到ArrayList中,該項目是null值..

MainActivity.java

1 package com.example.digesh.divinecart; 
2  
3 import android.os.Bundle; 
4 import android.support.v7.app.AppCompatActivity; 
5 import android.widget.GridView; 
6  
7 import com.firebase.client.ChildEventListener; 
8 import com.firebase.client.DataSnapshot; 
9 import com.firebase.client.Firebase; 
10 import com.firebase.client.FirebaseError; 
11 import com.google.firebase.database.FirebaseDatabase; 
12  
13 import java.util.ArrayList; 
14  
15 public class MainActivity extends AppCompatActivity { 
16  
17  Item i = new Item(); 
18  GridView gridView; 
19  ArrayList<Item> items = new ArrayList<Item>(); 
20  itemAdapter itemadapter; 
21  FirebaseDatabase database; 
22  //DatabaseReference mRef; 
23  Firebase mRef; 
24  
25  @Override 
26  protected void onCreate(Bundle savedInstanceState) { 
27   super.onCreate(savedInstanceState); 
28   setContentView(R.layout.activity_main); 
29   Firebase.setAndroidContext(this); 
30  
31   //mRef = FirebaseDatabase.getInstance().getReference(); 
32  
33   //mRef = database.getReference(); 
34   mRef = new Firebase("https://divinecart-9b58e.firebaseio.com/"); 
35  
36  
37   /* mRef.addValueEventListener(new ValueEventListener() { 
38    @Override 
39    public void onDataChange(DataSnapshot dataSnapshot) { 
40     itemName = dataSnapshot.getValue(String.class); 
41     name1 = (TextView)findViewById(R.id.name); 
42     name1.setText(itemName); 
43  
44    } 
45  
46    @Override 
47    public void onCancelled(FirebaseError firebaseError) { 
48  
49    } 
50   });*/ 
51  
52  
53   gridView = (GridView) findViewById(R.id.gridview); 
54  
55  
56   // itemAdapter itemadapter = new itemAdapter(MainActivity.this,image,itemName,itemPrice); 
57   itemadapter = new itemAdapter(MainActivity.this, getItems()); 
58  
59  
60   gridView.setAdapter(itemadapter); 
61   /*gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
62    @Override 
63    public void onItemClick(AdapterView<?> parent, View view, int position, long l) { 
64     //String selectedItem = parent.getItemAtPosition(position).toString(); 
65     Intent i = new Intent(getApplicationContext(),itemdetails.class); 
66     i.putExtra("Position",position); 
67     startActivity(i); 
68  
69     //Toast.makeText(getApplicationContext(),itemName[position],Toast.LENGTH_LONG).show(); 
70     Toast.makeText(getApplicationContext(),itemName[position],Toast.LENGTH_LONG).show(); 
71  
72    } 
73   });*/ 
74  } 
75  
76  public void fetchData(DataSnapshot ds) { 
77   items.clear(); 
78  
79   for (DataSnapshot data : ds.getChildren()) { 
80    //i = new Item(); 
81    //i = data.getValue(Item.class); 
82    i = (Item) data.getValue(Item.class); 
83    //i = (Item)data.getValue(Item.class); 
84  
85  
86    items.add(i); 
87    //System.out.println(i.getName()); 
88    //items.add(i.getName().toString(),i.getPrice().toString()); 
89   } 
90   itemadapter.notifyDataSetChanged(); 
91  
92  } 
93  
94  public ArrayList<Item> getItems() { 
95   mRef.addChildEventListener(new ChildEventListener() { 
96    @Override 
97    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) { 
98  
99     fetchData(dataSnapshot); 
100     itemadapter.notifyDataSetChanged(); 
101    } 
102 
103    @Override 
104    public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
105 
106     fetchData(dataSnapshot); 
107     itemadapter.notifyDataSetChanged(); 
108 
109    } 
110 
111    @Override 
112    public void onChildRemoved(DataSnapshot dataSnapshot) { 
113 
114    } 
115 
116    @Override 
117    public void onChildMoved(DataSnapshot dataSnapshot, String s) { 
118 
119    } 
120 
121    @Override 
122    public void onCancelled(FirebaseError firebaseError) { 
123 
124    } 
125   }); 
126   return items; 
127  } 
128 
129 
130 } 
131 

Item.java

1 package com.example.digesh.divinecart; 
2  
3 import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
4  
5 /** 
6  * Created by Digesh on 26-10-16. 
7  */ 
8 @JsonIgnoreProperties(ignoreUnknown = true) 
9 public class Item { 
10  //private int image; 
11  private String name; 
12  private String price; 
13  
14  public Item() { 
15  
16  
17  } 
18  
19  public Item(String name, String price){ 
20   //this.image = image; 
21   super(); 
22   this.name = name; 
23   this.price = price; 
24  
25  } 
26  
27  
28  
29  /* public int getImage(){ 
30    return image; 
31   } 
32   public void setImage(int image){ 
33    this.image = image; 
34   } 
35  */ 
36  public String getName(){ 
37   return name; 
38  } 
39  public void setName(String name){ 
40   this.name = name; 
41  } 
42  
43  public String getPrice(){ 
44   return price; 
45  } 
46  public void setPrice(String price){ 
47   this.price = price; 
48  } 
49 } 
50 
+0

什麼問題? '(Item)data.getValue(Item.class);'返回null? –

+0

yes ... i =(Item)data.getValue(Item.class);一片空白。我可以在快照中看到corrent數據..但是我爲空... –

+0

好吧,我們無法看到您的Firebase數據,因此有關'data.getValue'的一些問題無法正確將您的快照數據映射到您的Java對象 –

回答

0

看起來您最初使用了新的9.x.x SDK,然後評論了大部分代碼,現在正在使用舊版2.5.x SDK。但是您必須在build.gradle文件中留下對新SDK的引用,例如compile 'com.google.firebase:firebase-database:9.8.0',因爲您的代碼包含聲明com.google.firebase.database.FirebaseDatabase。舊版SDK和新SDK不兼容,不能在同一版本中使用。這可能是你觀察到的問題的原因。選擇一個SDK或另一個,然後更新您的build.gradle和代碼以僅使用一個。有關詳細信息,請參閱Firebase Upgrade Guide

+0

我嘗試了一切,但仍然不能正常工作...項目爲空... –

0

你的問題是在你的物品類。你必須請求中最多的數據庫完全值匹配的值,如下

public class Item { 
10  //private int image; 
11  private String itemName; 
12  private String itemPrice; 
13  
14  public Item() { 
15  
16  
17  } 
18  
19  public Item(String itemName, String itemPrice){ 
20   //this.image = image; 
21   super(); 
22   this.itemName = itemName; 
23   this.itemPrice = itemPrice; 
24  
25  } 
26  
27  
28  
29  /* public int getImage(){ 
30    return image; 
31   } 
32   public void setImage(int image){ 
33    this.image = image; 
34   } 
35  */ 
36  public String getItemName(){ 
37   return itemName; 
38  } 
39  public void setName(String name){ 
40   this.itemName = itemName; 
41  } 
42  
43  public String getItemPrice(){ 
44   return itemPrice; 
45  } 
46  public void setItemPrice(String itemPrice){ 
47   this.itemPrice = itemPrice; 
48  } 
49 } 

這應該使數據快照工作類應該改變。當該項目類從數據庫中提取數據時,名稱必須完全相同,以便知道要提取的內容。

+0

其仍然不能正常工作 –

+0

得到了解決方案..感謝您的幫助朋友.. –

+1

Item i = dataSnapshot1。的getValue(Item.class); String fname = i.itemName; String fprice =「Rs。」+ i.itemPrice; items.add(new Item(fname,fprice)); –