加载购物车数据时显示空白字段 [英] Showing empty field while loading cart data

查看:78
本文介绍了加载购物车数据时显示空白字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个食品订购应用程序上的项目。在这里,我添加了一个功能,例如用户可以在nav_order部分上查看其订单。但是,当我单击此部分时,它看不到任何数据,但购物车上空白。

I am doing a project on a food ordering app. Here,I have added a feature like an user can view their order on nav_order section. But when i click on this section, it can't see any kind of data but blank on cart.

这是我的order_layout.xml

Here is my 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

Here is my 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

Here is my 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类

Here is my Request.java class

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类

Here is my Home.java class

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

Here is my 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);

    }
}

但我的购物车仍在显示这样的黑屏:

But Still my cart is still showing the blank screen like this:

但是应该是这样的:

But should be something like this:

请帮助我。我已经坚持了三天。我的项目截止日期就摆在我面前。请有人,帮我解决这个问题。

Please help me. I have stuck on this for three days long. And my project deadline hanging in front of me. please someone guys, help me outta this.

@AlexMamo这是我的JSON文件

@AlexMamo Here is my JSON file

{
  "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"
          }}}

这是我的数据库的屏幕截图:

Here is the Screen shot of my database:

这是我在SQLite的数据库浏览器上的OrderDetailID表:

Here is my Table for OrderDetailID on DB Browser for SQLite:

CREATE TABLE `OrderDetailID` (
    `ID`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    `ProductId` TEXT,
    `ProductName`   TEXT,
    `Quantity`  TEXT,
    `Price` TEXT,
    `Discount`  TEXT
);


推荐答案

在您发布的代码中,命名为 OrderViewHolder类代码 onCreateViewHolder您返回的是null,因此没有附加视图,因此它什么也不显示。

In your posted code naming 'OrderViewHolder class code' in onCreateViewHolder you are returning null, so no view is attached and hence it is showing nothing.

这篇关于加载购物车数据时显示空白字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆