parent
85314555a8
commit
04bfc2cd96
@ -0,0 +1,151 @@
|
||||
package com.example.drink_order_system;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.RestaurantViewHolder> {
|
||||
private ArrayList<Restaurant> mList;
|
||||
private final LayoutInflater mLayoutInflater;
|
||||
private MyClickListener mListener;
|
||||
private OnItemClickListener itemClickListener;
|
||||
|
||||
|
||||
public RestaurantAdapter(LayoutInflater layoutInflater, ArrayList<Restaurant> mList) {
|
||||
this.mList = mList;
|
||||
mLayoutInflater = layoutInflater;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestaurantViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View itemView = mLayoutInflater.inflate(R.layout.restaurant_item, parent, false);
|
||||
return new RestaurantViewHolder(itemView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RestaurantViewHolder holder, int position) {
|
||||
|
||||
Restaurant target = getItem(position);
|
||||
if (holder instanceof RestaurantAdapter.RestaurantViewHolder) {
|
||||
((RestaurantAdapter.RestaurantViewHolder) holder).bindBean(target);
|
||||
|
||||
|
||||
} else {
|
||||
throw new IllegalStateException("Illegal state Exception onBindviewHolder");
|
||||
}
|
||||
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (itemClickListener != null) {
|
||||
itemClickListener.onItemClick(v, holder.getAdapterPosition());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
new AsyncTask<Void, Void, Bitmap>() {
|
||||
@Override
|
||||
protected Bitmap doInBackground(Void... voids) {
|
||||
try {
|
||||
URL url = new URL(target.get_imagePath());
|
||||
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Bitmap bitmap) {
|
||||
if (bitmap != null) {
|
||||
holder.imageView.setImageBitmap(bitmap);
|
||||
}
|
||||
}
|
||||
}.execute();
|
||||
Log.e("imagepath1",target.get_imagePath());
|
||||
}
|
||||
|
||||
public void buttonSetOnClick(MyClickListener mListener) {
|
||||
this.mListener = mListener;
|
||||
}
|
||||
|
||||
public void setRestaurantList(ArrayList<Restaurant> newList) {
|
||||
mList = newList;
|
||||
notifyDataSetChanged(); // 通知适配器数据已更改
|
||||
}
|
||||
|
||||
public interface MyClickListener {
|
||||
void onclick(View v, int position);
|
||||
}
|
||||
public interface OnItemClickListener {
|
||||
void onItemClick(View view, int position);
|
||||
}
|
||||
public void setOnItemClickListener(OnItemClickListener listener) {
|
||||
this.itemClickListener = listener;
|
||||
}
|
||||
private Restaurant getItem(int position) {
|
||||
return mList.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mList.size();
|
||||
}
|
||||
|
||||
class RestaurantViewHolder extends RecyclerView.ViewHolder {
|
||||
private final TextView name;
|
||||
private final TextView introduction;
|
||||
private final TextView volume;
|
||||
private final ImageView imageView;
|
||||
RestaurantViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
name = itemView.findViewById(R.id.Text_name);
|
||||
introduction = itemView.findViewById(R.id.Text_introduction);
|
||||
volume = itemView.findViewById(R.id.Text_volume);
|
||||
imageView = itemView.findViewById(R.id.Text_image);
|
||||
/*itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// 获取当前位置的店铺对象
|
||||
Restaurant clickedRestaurant = getItem(getAdapterPosition());
|
||||
|
||||
// 根据 clickedRestaurant 的信息跳转到对应的店铺页面
|
||||
// 这里你可以使用 Intent 或其他导航方式来实现跳转
|
||||
// 例如:
|
||||
// Intent intent = new Intent(itemView.getContext(), ShopActivity.class);
|
||||
// intent.putExtra("restaurantId", clickedRestaurant.getId());
|
||||
// itemView.getContext().startActivity(intent);
|
||||
Intent intent = new Intent(itemView.getContext(), OrderActivity.class);
|
||||
itemView.getContext().startActivity(intent);
|
||||
}
|
||||
});*/
|
||||
}
|
||||
|
||||
void bindBean(final Restaurant bean) {
|
||||
name.setText(bean.get_name());
|
||||
introduction.setText(bean.get_introduction());
|
||||
volume.setText(String.format("¥ %d", (bean.get_volume())));
|
||||
// Picasso.get().load(bean.get_imagePath()).into(imageView);
|
||||
Log.e("imagepath2",bean.get_imagePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue