我能够从Firebase读取数据但是当我尝试将值(Item)添加到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 :(得分:0)
您似乎最初使用了新的9.x.x SDK,然后将大部分代码注释掉,现在使用的是旧版2.5.x SDK。但是您必须在build.gradle文件中保留对新SDK的引用,例如com.google.firebase.database.FirebaseDatabase
,因为您的代码包含id_token
的声明。旧版SDK和新SDK不兼容,不能在同一版本中使用。这可能是您正在观察的问题的原因。选择一个SDK或另一个SDK并更新build.gradle和代码以仅使用一个。有关详细信息,请参阅Firebase Upgrade Guide。
答案 1 :(得分:0)
您的问题出在您的Item类中。您请求的值必须完全匹配数据库中的值,该类应更改如下
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 }
这应该使数据快照起作用。当该Item Class从数据库中提取数据时,名称必须完全相同,以便知道要拉什么。