我试图创建一个resturant apk,它在这个菜单中有一个菜单你选择了什么样的食物你想说如果你想要一个披萨,当你按披萨它会打开另一个菜单与不同种类的披萨,按下我想要的披萨,应该带你去另一个活动,并告诉你披萨的细节,但它没有显示我,我不知道是什么问题
这是我的食物清单活动
package com.example.median1.demo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class FoodLIst extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference foodList;
String categoryId;
FirebaseRecyclerAdapter<Food,FoodViewHolder>adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
//firebase
database=FirebaseDatabase.getInstance();
foodList= database.getReference("Foods");
recyclerView=(RecyclerView)findViewById(R.id.recycler_food);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//get food list intent
if(getIntent()!=null){
categoryId=getIntent().getStringExtra("CategoryId");
if(!categoryId.isEmpty()&& categoryId!=null){
loadListFood(categoryId);
}
}
}
private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,
R.layout.food_item,
FoodViewHolder.class,
foodList.orderByChild("MenuId").equalTo(categoryId)) {
@Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
viewHolder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(viewHolder.food_image);
final Food local=model;
viewHolder.setItemClicklistener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
// start new activity for the food destails
Intent foodDetails=new Intent(FoodLIst.this,FoodDetail.class);
foodDetails.putExtra("FoodId",adapter.getRef(position).getKey()); //send foood id to new activity
startActivity(foodDetails);
}
});
}
};
//set adapter
Log.d("TAG",""+adapter.getItemCount());
recyclerView.setAdapter(adapter);
}
}
这是我的食物详情活动
package com.example.median1.demo;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.io.File;
public class FoodDetail extends AppCompatActivity {
TextView food_name,food_price,food_description;
ImageView food_image;
CollapsingToolbarLayout collapsingToolbarLayout;
FloatingActionButton btnCart;
ElegantNumberButton numberButton;
String foodId="";
FirebaseDatabase database;
DatabaseReference foods;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
//firebase
database=FirebaseDatabase.getInstance();
foods=database.getReference("Foods");
//int View
numberButton=(ElegantNumberButton)findViewById(R.id.number_button);
btnCart=(FloatingActionButton)findViewById(R.id.btnCart);
food_description=(TextView)findViewById(R.id.food_description2);
food_name=(TextView)findViewById(R.id.food_name);
food_price=(TextView)findViewById(R.id.food_price);
food_image=(ImageView)findViewById(R.id.img_food);
collapsingToolbarLayout=(CollapsingToolbarLayout)findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
//get food id from the intent
if(getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if(foodId.isEmpty()){
getDetailFood(foodId);
}
}
private void getDetailFood(String foodId) {
foods.child(foodId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Food food=dataSnapshot.getValue(Food.class);
//set image
Picasso.with(getBaseContext()).load(food.getImage()).into(food_image);
collapsingToolbarLayout.setTitle(food.getName());
food_price.setText(food.getPrice());
food_name.setText(food.getName());
food_description.setText(food.getDescription());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
这是食物细节的食物类,我从火堆中找回所有孩子
package com.example.median1.demo;
/**
* Created by median1 on 11/5/2017.
*/
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;
}
}
这是食物细节的xml 我不知道它是否重要
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.median1.demo.FoodDetail">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
>
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#0e0d0e"
app:expandedTitleTextAppearance="@android:color/transparent">
<ImageView
android:id="@+id/img_food"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:title="food name"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btnCart"
android:src="@drawable/ic_shopping_cart_black_24dp"
android:backgroundTint="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="6dp"
app:pressedTranslationZ="12dp"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom|right|end"
app:useCompatPadding="true"
/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:clipToPadding="false"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/food_name"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="Food name "
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/layout_price"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/ic_attach_money_black_24dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/food_price"
android:text="1,000 "
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="wrap_content" />
</LinearLayout>
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:layout_width="100dp"
android:layout_height="30dp"
android:id="@+id/number_button"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="18dp"
app:textSize="8sp"
app:backGroundColor="@color/colorAccent"
app:initialNumber="1"
app:finalNumber="20"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/food_description2"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="description"
android:textColor="@android:color/black"
android:textSize="14sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Firebase数据库
答案 0 :(得分:0)
你不能在logcat或运行中看到错误吗?我认为错误在这里:
//get food id from the intent
if(getIntent()!=null)
foodId=getIntent().getStringExtra("FoodId");
if(foodId.isEmpty()){
getDetailFood(foodId);
}
}
或在这里
//get food list intent
if(getIntent()!=null){
categoryId=getIntent().getStringExtra("CategoryId");
if(!categoryId.isEmpty()&& categoryId!=null){
loadListFood(categoryId);
}
}
}
也许在点击时检查运行并且问题恰好在哪里找到问题..你可能会得到一个空字符串
编辑:我让同一个应用程序遇到了同样的问题,在我的运行中它说问题在这里if(!categoryId.isEmpty()&& categoryId!=null