Я делаю проект для приложения заказа еды. Здесь я добавил функцию, позволяющую пользователю просматривать их заказ в разделе nav_order. Но когда я нажимаю на этот раздел, он не видит никаких данных, кроме пустых в корзине.
Вот мой order_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardElevation="4dp"
>
<LinearLayout
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_weight="9"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:text="#111111"
android:textStyle="bold"
android:textAllCaps="true"
android:layout_gravity="center_vertical|start"
android:layout_marginLeft="10dp"
android:id="@+id/order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="Status"
android:textStyle="italic"
android:textAllCaps="true"
android:layout_gravity="center_vertical|start"
android:layout_marginLeft="10dp"
android:id="@+id/order_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="1234567"
android:textStyle="italic"
android:textAllCaps="true"
android:layout_gravity="center_vertical|start"
android:layout_marginLeft="10dp"
android:id="@+id/order_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="Address"
android:textStyle="italic"
android:textAllCaps="true"
android:layout_gravity="center_vertical|start"
android:layout_marginLeft="10dp"
android:id="@+id/order_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Вот мой Activity_order_status.java
package com.example.androideatit;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.androideatit.Common.Common;
import com.example.androideatit.Interface.ItemClickListener;
import com.example.androideatit.Model.Request;
import com.example.androideatit.ViewHolder.OrderViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class OrderStatus extends AppCompatActivity {
public RecyclerView.LayoutManager layoutManager;
public RecyclerView recyclerView;
FirebaseDatabase database;
DatabaseReference requests;
FirebaseRecyclerAdapter<Request, OrderViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_status);
//Firebase
database= FirebaseDatabase.getInstance();
requests=database.getReference("Requests");
recyclerView = findViewById(R.id.listOrders);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
loadOrders(Common.currentUser.getPhone());
}
private void loadOrders(String phone) {
final FirebaseRecyclerOptions<Request>options;
options=new FirebaseRecyclerOptions.Builder<Request>()
.setQuery(requests,Request.class).build();
adapter=new FirebaseRecyclerAdapter<Request,OrderViewHolder>(options){
@NonNull
@Override
public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.order_layout,parent,false);
return new OrderViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull OrderViewHolder orderViewHolder, int i, @NonNull Request request) {
orderViewHolder.txtOrderId.setText(adapter.getRef(i).getKey());
orderViewHolder.txtOrderStatus.setText(convertTostatus(request.getStatus()));
orderViewHolder.txtOrderAddress.setText(request.getAddress());
orderViewHolder.txtOrderPhone.setText(request.getPhone());
final Request local=request;
orderViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLoingClick) {
Intent orderDetail=new Intent(OrderStatus.this,OrderViewHolder.class);
orderDetail.putExtra("phone",adapter.getRef(position).getKey());
startActivity(orderDetail);
}
});
}
};
recyclerView.setAdapter(adapter);
}
private String convertTostatus(String status) {
if (status.equals("0"))
return "Placed";
else if (status.equals("1"))
return "On my way";
else
return "Shipped";
}
}
Вот мой Activity_order_status.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/background"
tools:context=".OrderStatus">
<androidx.recyclerview.widget.RecyclerView
android:background="@android:color/transparent"
android:id="@+id/listOrders"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
Вот мой класс Request.java
public class Request{
public class Request{
private String phone;
private String name;
private String address;
private String total;
private String status;
private List<Order>foods;
public Request() {
}
public Request(String phone, String name, String address, String total, List<Order> foods) {
this.phone = phone;
this.name = name;
this.address = address;
this.total = total;
this.foods = foods;
this.status="0"; //Default is 0; 0:Placed , 1:Shipping, 2:Shipped
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public List<Order> getFoods() {
return foods;
}
public void setFoods(List<Order> foods) {
this.foods = foods;
}
}
Вот мой класс Home.java
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
LinearLayoutManager layoutManager;
FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Init Firebase
database = FirebaseDatabase.getInstance();
category = database.getReference("Category");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cartIntent=new Intent(Home.this,Cart.class);
startActivity(cartIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Set Name for User
View headerView = navigationView.getHeaderView(0);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
//Load Menu
recycler_menu = (RecyclerView)findViewById(R.id.recyler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
}
private void loadMenu() {
FirebaseRecyclerOptions<Category> options;
// FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter;
options = new FirebaseRecyclerOptions.Builder<Category>()
.setQuery(category,Category.class).build();
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull MenuViewHolder menuViewHolder, int i, @NonNull Category category) {
Picasso.get().load(category.getImage())
.into(menuViewHolder.imageView, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
});
menuViewHolder.textMenuName.setText(category.getName());
final Category clickItem = category;
menuViewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent foodList=new Intent(Home.this,FoodList.class);
//Because CategoryID is key,so we just get key of this item
foodList.putExtra("CategoryId",adapter.getRef(position).getKey());
startActivity(foodList);
}
});
}
@NonNull
@Override
public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item,parent,false);
return new MenuViewHolder(view);
}
};
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
recycler_menu.setLayoutManager(gridLayoutManager);
adapter.startListening();
recycler_menu.setAdapter(adapter);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id==R.id.nav_menu) {
}else if(id==R.id.nav_cart){
Intent cartIntent=new Intent(Home.this,Cart.class);
startActivity(cartIntent);
}else if(id==R.id.nav_order){
Intent orderIntent=new Intent(Home.this,OrderStatus.class);
startActivity(orderIntent);
}else if(id==R.id.logout){
Intent signIn=new Intent(Home.this,Signin.class);
signIn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(signIn);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Вот мой OrderViewHolder.java
public class OrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtOrderId,txtOrderStatus,txtOrderPhone,txtOrderAddress;
private ItemClickListener itemClickListener;
public OrderViewHolder(@NonNull View itemView) {
super(itemView);
txtOrderAddress=itemView.findViewById(R.id.order_address);
txtOrderId=itemView.findViewById(R.id.order_id);
txtOrderStatus=itemView.findViewById(R.id.order_status);
txtOrderPhone=itemView.findViewById(R.id.order_phone);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
}
Но все равно моя корзина по-прежнему показывает пустой экран, как это: ![enter image description here](https://i.stack.imgur.com/bnJuQ.png)
Но следуетбыть примерно таким: ![enter image description here](https://i.stack.imgur.com/ZQ9Ai.png)
Пожалуйста, помогите мне. Я застрял на этом в течение трех дней. И мой проект крайний срок висит передо мной. пожалуйста, кто-нибудь, ребята, помогите мне с этим.
@ AlexMamo Вот мой файл JSON
{
"Category": {
"10": {
"Name": "Noodles",
"Image": "http://lazeezpune.com/wp-content/uploads/2018/01/chicken-triple-hakka-noodles.jpg"
},
"11": {
"Name": "Snacks",
"Image": "http://www.rewardsnetwork.com/wp-content/uploads/2017/12/SnacksAsDaypart_2.jpg"
},
"01": {
"Name": "FINGER FOODS",
"Image": "https://bmexdi064h-flywheel.netdna-ssl.com/wp-content/uploads/2018/08/Italian-Chicken-Wrap-foodiecrush.com-028.jpg"
},
"02": {
"Name": "Western Soup",
"Image": "https://www.theseasonedmom.com/wp-content/uploads/2018/10/Grandmothers-Hamburger-Soup-TEXT.jpg"
},
"03": {
"Name": "Medifoods delights",
"Image": "https://www.kindmeal.my/photos/deal/3/358-1030-l.jpg"
},
"04": {
"Name": "Sandwitches",
"Image": "https://www.landolakes.com/RecipeManagementSystem/media/Recipe-Media-Files/Recipes/Retail/x17/20733-all-american-club-sandwich-600x600.jpg?ext=.jpg"
},
"05": {
"Name": "Pizza",
"Image": "https://img1.wsimg.com/isteam/stock/2999/:/"
},
"06": {
"Name": "Pasta",
"Image": "https://www.dinneratthezoo.com/wp-content/uploads/2018/07/penne-alla-vodka-5.jpg"
},
"07": {
"Name": "Chicken",
"Image": "https://www.fifteenspatulas.com/wp-content/uploads/2018/04/Korean-Chicken-Wings-Fifteen-Spatulas-6.jpg"
},
"08": {
"Name": "Fish",
"Image": "https://img.taste.com.au/pUWqjn9Q/taste/2016/11/chargrilled-fish-with-green-chilli-coriander-and-coconut-relish-70446-1.jpeg"
},
"09": {
"Name": "Chinese Vegiterian",
"Image": "https://cdn.eventfinda.co.nz/uploads/events/transformed/1168962-522855-34.jpg?v=6"
}
},
"User": {
"0988123344": {
"Name": "Eddydn",
"Password": "1234"
},
"0988123355": {
"Name": "EddyLEe",
"Password": "1234"
},
"0988123388": {
"Name": "Tom cruise",
"Password": "12345"
}
},
"Foods" : {
"01" : {
"description" : "",
"discount" : "0",
"image" : "https://live.staticflickr.com/65535/48581150471_c644954e41_o.jpg",
"menuId" : "11",
"name" : "GINGER PAO",
"price" : "1000"
},
"02" : {
"description" : "",
"discount" : "0",
"image" : "https://live.staticflickr.com/65535/48581150436_dca091792a_o.jpg",
"menuId" : "11",
"name" : "COCONUT PAO",
"price" : "1000"
},
"03" : {
"description" : "",
"discount" : "0",
"image" : "https://live.staticflickr.com/65535/48581150341_b2608d308f_o.jpg",
"menuId" : "11",
"name" : "RED BEAN PAO",
"price" : "1000"
}}}
Вот снимок экрана моей базы данных: ![enter image description here](https://i.stack.imgur.com/uxqvY.png)
Вот моя таблицадля OrderDetailID в браузере БД для SQLite:
CREATE TABLE `OrderDetailID` (
`ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`ProductId` TEXT,
`ProductName` TEXT,
`Quantity` TEXT,
`Price` TEXT,
`Discount` TEXT
);