You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
6.2 KiB

package com.example.drink_order_system;
import static com.example.drink_order_system.Restaurant.all_restaurant;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* A simple {@link Fragment} subclass.
* Use the {@link RestaurantFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class RestaurantFragment extends Fragment {
private ArrayList<Restaurant> restaurant_array = new ArrayList<Restaurant>();
private RestaurantAdapter Adapter;
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private LinearLayoutManager llM;
private SearchView searchView;
private Context mContext = this.getActivity();
private RecyclerView listView;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private String userName;
@SuppressLint("ValidFragment")
public RestaurantFragment(String userName) {
// Required empty public constructor
this.userName=userName;
}
public RestaurantFragment() {
// Required empty public constructor
}
public static RestaurantFragment newInstance(String param1, String param2) {
RestaurantFragment fragment = new RestaurantFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_restaurant, container, false);
SearchView mSearch = (SearchView) view.findViewById(R.id.my_search);
listView = (RecyclerView) view.findViewById(R.id.RV_restaurant);
searchView = (SearchView) view.findViewById(R.id.my_search);
initData();
llM = new LinearLayoutManager(this.getActivity());
listView.setLayoutManager(llM);
Adapter = new RestaurantAdapter(inflater, all_restaurant);
Adapter.setOnItemClickListener(new RestaurantAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// 获取点击的餐厅对象
Restaurant clickedRestaurant = all_restaurant.get(position);
String restaurantName = clickedRestaurant.get_name(); // 获取餐厅名称
// 创建 Intent 并传递餐厅名称到 OrderActivity
Intent intent = new Intent(getActivity(), OrderActivity.class);
intent.putExtra("userName", userName);
intent.putExtra("restaurantName", restaurantName);
startActivity(intent);
}
});
listView.setAdapter(Adapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
// 在搜索文本变化监听中更新数据源
@Override
public boolean onQueryTextChange(String queryText) {
ArrayList<Restaurant> filteredRestaurants = new ArrayList<>();
for (Restaurant restaurant : restaurant_array) {
if (restaurant.get_name().contains(queryText)) {
filteredRestaurants.add(restaurant);
}
}
// 更新适配器数据源
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Adapter.setRestaurantList(filteredRestaurants);
}
});
return true;
}
});
return view;
}
private void initData() {
String url="http://192.168.43.176:8080/warehouse_management_system_war_exploded/getStore";
// new Restaurant("a", 100,
// "川菜馆", url);
restaurant_array = all_restaurant;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
Log.i("responseDataR","fail");
// 处理请求失败
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
Log.i("responseDataR",responseData);
Gson gson = new Gson();
Type restaurantListType = new TypeToken<ArrayList<Restaurant>>() {}.getType();
final ArrayList<Restaurant> restaurants = gson.fromJson(responseData, restaurantListType);
getActivity().runOnUiThread(() -> {
all_restaurant.clear();
all_restaurant.addAll(restaurants);
Adapter.notifyDataSetChanged();
});
} else {
// 处理请求失败
}
}
});
}
}