Выборка из базы данных Firebase
02.05.2018, 22:57. Показов 2779. Ответов 0
Доброго времени суток. Не так давно начал изучать программирование на андроид, решил запилить приложение с обращением к базе данных Firebase. Столкнулся с такой проблемой: Не могу сделать выборку по Id из двух таблиц. Сколько не гуглил, ответа так и не нашел. Ошибка: " actual and formal argument lists differ in length". Код класса с ошибкой:
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
| public class FoodList extends AppCompatActivity {
private TextView textView;
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
DatabaseReference food;
String menuId = "";
FirebaseRecyclerAdapter<Food,FoodViewHolder> adapter;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
recyclerView = (RecyclerView)findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
if (getIntent() != null)
menuId = getIntent().getStringExtra("MenuId");
if (!menuId.isEmpty() && menuId != null) {
loadListFood(menuId);
}
}
private void loadListFood(String menuId) {
Query query = FirebaseDatabase
.getInstance()
.getReference()
.child("Food");
FirebaseRecyclerOptions<Food> options =
new FirebaseRecyclerOptions.Builder<Food>()
.setQuery(query, Food.class)
.build();
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>
(Food.class,
R.layout.food_item,
FoodViewHolder.class,
food.orderByChild("MenuId").equalTo("RestId"))
{
@Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_item, parent, false);
return new FoodViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder holder, int position, @NonNull Food model) {
holder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(holder.food_image);
final Food clickItem = model;
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
// Toast.makeText(FoodList.this, ""+clickItem.getName(), Toast.LENGTH_SHORT).show();
//Get Restaurant and send to new Activity/Fragment
Intent foodDetail = new Intent(FoodList.this, FoodDetail.class);
//RestaurantId is key, so we just get key of this item
foodDetail.putExtra("FoodId", adapter.getRef(position).getKey());
startActivity(foodDetail);
}
});
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
} |
|
Код класса Food:
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| public class Food {
private String Name, Image, Description, Price, Discount, MenuId;
public Food() {
}
public Food(String name, String image, String description, String price, String discount, String menuId) {
Name = name;
Image = image;
Description = description;
Price = price;
Discount = discount;
MenuId = menuId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getDiscount() {
return Discount;
}
public void setDiscount(String discount) {
Discount = discount;
}
public String getMenuId() {
return MenuId;
}
public void setMenuId(String menuId) {
MenuId = menuId;
} |
|
Код класса Category:
Java | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| public class Category {
private String Name, Image, RestId;
public Category(){}
public Category(String name, String image, String restId) {
Name = name;
Image = image;
RestId = restId;
}
public String getRestId() {
return RestId;
}
public void setRestId(String restId) {
RestId = restId;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
} |
|
То есть выборку необходимо сделать из таблицы Category и Food, далее сравнить их и вывести соответствующие данные на экран. Может кто сталкивался с данной проблемой или есть мысли как это можно решить...Please Help!
P.S. Кусочек кода с выборкой (увы, не работающей):
Java | 1
2
3
4
5
6
7
8
9
| FirebaseRecyclerOptions<Food> options =
new FirebaseRecyclerOptions.Builder<Food>()
.setQuery(query, Food.class)
.build();
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>
(Food.class,
R.layout.food_item,
FoodViewHolder.class,
food.orderByChild("MenuId").equalTo("RestId")) |
|
Добавлено через 12 минут
Логи ошибки:
Prolog | 1
2
3
4
5
6
7
| error: constructor FirebaseRecyclerAdapter in class FirebaseRecyclerAdapter<T,VH> cannot be applied to given types;
required: FirebaseRecyclerOptions<Food>
found: Class<Food>,int,Class<FoodViewHolder>,Query
reason: actual and formal argument lists differ in length
where T,VH are type-variables:
T extends Object declared in class FirebaseRecyclerAdapter
VH extends ViewHolder declared in class FirebaseRecyclerAdapter |
|
Примерный вид таблицы:
JSON | 1
2
3
4
5
6
7
8
9
10
11
12
13
| "Food": {
"02": {
"Name": "Кокосовый пирожок",
"Image": "http://medifoods.my/images/menu/p2_coconut_pao.jpg",
"Description": "Вкуснейшее восточное лакомство с нежным кокосом доставит вам незабываемое удовольствие",
"Price": "600",
"Discount": "0",
"MenuId": "11"
},
"Categories": {
"11": {
"Name": "Закуски",
"Image": "http://medifoods.my/wp-content/uploads/2015/03/cover-menu-snacks.jpg", |
|
0
|