From a214be6406895b98a4a1a9f03e5d19d9e9b987e2 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 23 Dec 2024 01:03:20 +0800 Subject: [PATCH] =?UTF-8?q?Android=E4=BB=A3=E7=A0=8112=E6=9C=8823=E6=97=A5?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/coursedesign/Api.java | 6 +++ .../example/coursedesign/LoginActivity.java | 3 -- .../example/coursedesign/MineFragment.java | 8 +++ .../coursedesign/NotOnSaleFragment.java | 38 ++++++++++++++ .../example/coursedesign/OnSaleFragment.java | 49 +++++++++++++++++++ 5 files changed, 101 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/coursedesign/Api.java b/src/main/java/com/example/coursedesign/Api.java index d340c30..d045ad8 100644 --- a/src/main/java/com/example/coursedesign/Api.java +++ b/src/main/java/com/example/coursedesign/Api.java @@ -6,10 +6,12 @@ import java.util.Map; import retrofit2.Call; import retrofit2.http.Body; +import retrofit2.http.DELETE; import retrofit2.http.GET; import retrofit2.http.Header; import retrofit2.http.POST; import retrofit2.http.PUT; +import retrofit2.http.Path; import retrofit2.http.Query; public interface Api { @@ -53,4 +55,8 @@ public interface Api { //http://127.0.0.1:8080/system/commodity @POST("/system/commodity") Call addCommodity(@Header("Authorization") String token, @Body CommodityListResult.RowsDTO rowsDTO); + + //http://127.0.0.1:8080/system/commodity/{ids} + @DELETE("system/commodity/{ids}") + Call removeCommodity(@Header("Authorization") String token, @Path("ids") String ids); } diff --git a/src/main/java/com/example/coursedesign/LoginActivity.java b/src/main/java/com/example/coursedesign/LoginActivity.java index 572cfa3..128e6c2 100644 --- a/src/main/java/com/example/coursedesign/LoginActivity.java +++ b/src/main/java/com/example/coursedesign/LoginActivity.java @@ -14,9 +14,6 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; -import java.util.HashMap; -import java.util.Map; - import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; diff --git a/src/main/java/com/example/coursedesign/MineFragment.java b/src/main/java/com/example/coursedesign/MineFragment.java index 899109b..ef20c63 100644 --- a/src/main/java/com/example/coursedesign/MineFragment.java +++ b/src/main/java/com/example/coursedesign/MineFragment.java @@ -14,6 +14,8 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import com.bumptech.glide.Glide; + public class MineFragment extends Fragment { Button logOut; @@ -38,6 +40,12 @@ public class MineFragment extends Fragment { phone = view.findViewById(R.id.textView42); address = view.findViewById(R.id.textView43); + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + name.setText(sharedPreferences.getString("name", "")); + phone.setText(sharedPreferences.getString("phone", "")); + Glide.with(getContext()).load(sharedPreferences.getString("picture", "")).into(picture); + myOrder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/src/main/java/com/example/coursedesign/NotOnSaleFragment.java b/src/main/java/com/example/coursedesign/NotOnSaleFragment.java index e704f66..5d91ca7 100644 --- a/src/main/java/com/example/coursedesign/NotOnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/NotOnSaleFragment.java @@ -165,6 +165,44 @@ public class NotOnSaleFragment extends Fragment { } } }); + + holder.deleteButton.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + int adapterPosition = holder.getAdapterPosition(); + if(adapterPosition != RecyclerView.NO_POSITION){ + CommodityListResult.RowsDTO rowsDTO = result.rows.get(adapterPosition); + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + String ids = String.valueOf(rowsDTO.id); + + Call deleteCommodityCall = api.removeCommodity("Bearer ", ids); + deleteCommodityCall.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + + Result result = response.body(); + Log.e("error", result.msg); + if(result != null && result.code == 200){ + Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show(); + notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + } + } + }); } @Override diff --git a/src/main/java/com/example/coursedesign/OnSaleFragment.java b/src/main/java/com/example/coursedesign/OnSaleFragment.java index 3f4ef09..ba80666 100644 --- a/src/main/java/com/example/coursedesign/OnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/OnSaleFragment.java @@ -167,6 +167,55 @@ public class OnSaleFragment extends Fragment { } } }); + + holder.deleteButton.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + int adapterPosition = holder.getAdapterPosition(); + if(adapterPosition != RecyclerView.NO_POSITION){ + CommodityListResult.RowsDTO rowsDTO = result.rows.get(adapterPosition); + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + String ids = String.valueOf(rowsDTO.id); + + Call deleteCommodityCall = api.removeCommodity("Bearer ", ids); + deleteCommodityCall.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + + Result result = response.body(); + Log.e("error", result.msg); + if(result != null && result.code == 200){ + Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show(); + notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + } + } + }); + + holder.constraintLayout.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View v) { + int adapterPosition = holder.getAdapterPosition(); + if(adapterPosition != RecyclerView.NO_POSITION){ + CommodityListResult.RowsDTO rowsDTO = result.rows.get(adapterPosition); + + } + } + }); } @Override