如何选择一家餐厅,并从该餐厅获得唯一的特定食物?

时间:2018-12-12 17:31:50

标签: android

package com.example.a8users.foodcateringsystem.Restaurant;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.a8users.foodcateringsystem.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserDisplayRestaurantFragment extends Fragment {

    public UserDisplayRestaurantFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_display_restaurant, container, false);
        UserDisplayRestaurantBackgroundTask backgroundTaskRestaurant = new UserDisplayRestaurantBackgroundTask(getActivity(),view);
        backgroundTaskRestaurant.execute();
        // Inflate the layout for this fragment
        return view;
    }

}

package com.example.a8users.foodcateringsystem.Restaurant;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.example.a8users.foodcateringsystem.Cart.UserDisplayFoodFragment;
import com.example.a8users.foodcateringsystem.Cart.UserDisplayRestaurantFoodActivity;
import com.example.a8users.foodcateringsystem.Cart.UserViewCartActivity;
import com.example.a8users.foodcateringsystem.Constants;
import com.example.a8users.foodcateringsystem.MainActivity;
import com.example.a8users.foodcateringsystem.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class UserDisplayRestaurantBackgroundTask extends AsyncTask<Void, Restaurant, Void> implements RecyclerAdapterUserRestaurant.OnItemClickListener {
    Context ctx;
    Activity activity;
    RecyclerView recyclerView;
    RecyclerAdapterUserRestaurant adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<Restaurant> arrayList1 = new ArrayList<>();
    View rootView;

    public UserDisplayRestaurantBackgroundTask(Context ctx, View rootView){
        this.ctx = ctx;
        this.rootView = rootView;
    }

    @Override
    protected void onPreExecute() {

        recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerView1);
        layoutManager = new LinearLayoutManager(ctx);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new RecyclerAdapterUserRestaurant(arrayList1);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(UserDisplayRestaurantBackgroundTask.this);
    }

    @Override
    protected Void doInBackground(Void...params){
        try {
            URL url = new URL(Constants.URL_GET_RESTAURANT);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;

            while((line=bufferedReader.readLine())!=null){
                stringBuilder.append(line+"\n");
            }

            httpURLConnection.disconnect();
            String json_string = stringBuilder.toString().trim();
            JSONObject jsonObject = new JSONObject(json_string);
            JSONArray jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            while(count<jsonArray.length())
            {
                JSONObject JO = jsonArray.getJSONObject(count);
                count++;
                Restaurant restaurant = new Restaurant(
                        JO.getInt("restaurantID"),
                        JO.getString("name"),
                        JO.getString("description"),
                        JO.getString("location"),
                        JO.getString("status"));
                publishProgress(restaurant);
            }


            Log.d("JSON_STRING", json_string);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Restaurant...values) {
        arrayList1.add(values[0]);
        Log.i("Log value","onProgressUpdate"+values[0]);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

    @Override
    public void onItemClick(int position) {
        Intent detailIntent = new Intent(ctx,UserDisplayRestaurantFoodActivity.class);
        Restaurant clickedRestaurant = arrayList1.get(position);
        detailIntent.putExtra("RestaurantID", String.format("%d",clickedRestaurant.getRestaurantID()));
        detailIntent.putExtra("RestaurantName", clickedRestaurant.getName());
        detailIntent.putExtra("RestaurantDesc", clickedRestaurant.getDescription());
        detailIntent.putExtra("RestaurantLocation", clickedRestaurant.getLocation());
        detailIntent.putExtra("RestaurantStatus", clickedRestaurant.getStatus());
        ctx.startActivity(detailIntent);
    }
}

package com.example.a8users.foodcateringsystem.Cart;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.a8users.foodcateringsystem.MainActivity;
import com.example.a8users.foodcateringsystem.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserDisplayFoodFragment extends Fragment {

    private Button btnViewCart,btnCancel;
    public UserDisplayFoodFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_display_food, container, false);
        btnViewCart = (Button) view.findViewById(R.id.btnViewCart);
        btnCancel = (Button) view.findViewById(R.id.btnCancel);
        UserDisplayFoodBackgroundTask backgroundTaskFood = new UserDisplayFoodBackgroundTask(getActivity());
        backgroundTaskFood.execute();

        btnViewCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity() ,UserViewCartActivity.class));
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getFragmentManager().popBackStack();
                startActivity(new Intent(getActivity(), MainActivity.class));
            }
        });


        // Inflate the layout for this fragment
        return view;
    }

}

package com.example.a8users.foodcateringsystem.Cart;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import com.example.a8users.foodcateringsystem.Constants;
import com.example.a8users.foodcateringsystem.Food.Foods;
import com.example.a8users.foodcateringsystem.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class UserDisplayFoodBackgroundTask extends AsyncTask<Void, Foods, Void> implements RecyclerAdapterUserFood.OnItemClickListener {
    Context ctx;
    Activity activity;
    RecyclerView recyclerView;
    RecyclerAdapterUserFood adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<Foods> arrayList = new ArrayList<>();
    View rootView;

    public UserDisplayFoodBackgroundTask(Context ctx){
        this.ctx = ctx;
        this.rootView = rootView;
    }

    @Override
    protected void onPreExecute() {

        recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager(ctx);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new RecyclerAdapterUserFood(arrayList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(UserDisplayFoodBackgroundTask.this);
    }

    @Override
    protected Void doInBackground(Void...params){
        try {
            URL url = new URL(Constants.URL_GET_FOOD);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;

            while((line=bufferedReader.readLine())!=null){
                stringBuilder.append(line+"\n");
            }

            httpURLConnection.disconnect();
            String json_string = stringBuilder.toString().trim();
            JSONObject jsonObject = new JSONObject(json_string);
            JSONArray jsonArray = jsonObject.getJSONArray("server_response");
            int count = 0;
            while(count<jsonArray.length())
            {
                JSONObject JO = jsonArray.getJSONObject(count);
                count++;
                Foods foods = new Foods(
                        JO.getInt("foodID"),
                        JO.getString("name"),
                        JO.getString("photo"),
                        JO.getString("desc"),
                        JO.getString("type"),
                        JO.getString("category"),
                        Double.parseDouble(JO.getString("price")),
                        JO.getString("status"),
                        JO.getInt("restaurantID"));
                publishProgress(foods);
            }


            Log.d("JSON_STRING", json_string);

        } catch (MalformedURLException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Foods...values) {
        arrayList.add(values[0]);
        Log.i("Log value","onProgressUpdate"+values[0]);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }

    @Override
    public void onItemClick(int position) {
        Intent detailIntent = new Intent(ctx,UserDisplayFoodDetailActivity.class);
        Foods clickedFood = arrayList.get(position);
        detailIntent.putExtra("FoodID", String.format("%d",clickedFood.getFoodID()));
        detailIntent.putExtra("FoodName", clickedFood.getName());
        detailIntent.putExtra("FoodDescription", clickedFood.getDesc());
        detailIntent.putExtra("FoodType", clickedFood.getType());
        detailIntent.putExtra("FoodPrice", String.format("%.2f",clickedFood.getPrice()));
        detailIntent.putExtra("FoodCate", clickedFood.getCategory());
        detailIntent.putExtra("FoodStatus", clickedFood.getStatus());
        detailIntent.putExtra("picture", Base64.decode( clickedFood.getPicture(), Base64.DEFAULT));
        ctx.startActivity(detailIntent);
    }
}

你好,我是编程新手。我需要如何选择内部具有特定元素的项目的帮助?选择餐厅时,我需要帮助,但它会显示数据库中的所有食物。我如何选择餐厅而不是仅显示餐厅中的特定食物?如何选择一家餐厅,然后它将显示该餐厅的特定食物?

0 个答案:

没有答案