diff --git a/src/main/java/com/example/coursedesign/AddCommodityActivity.java b/src/main/java/com/example/coursedesign/AddCommodityActivity.java index cfdb8b9..5ec14bb 100644 --- a/src/main/java/com/example/coursedesign/AddCommodityActivity.java +++ b/src/main/java/com/example/coursedesign/AddCommodityActivity.java @@ -1,6 +1,16 @@ package com.example.coursedesign; +import android.app.Activity; +import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; +import android.widget.TextView; +import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; @@ -8,8 +18,25 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + public class AddCommodityActivity extends AppCompatActivity { + TextView back; + EditText nameET, priceET, descriptionET; + Spinner spinner; + Button addCommodityBtn; + + CommodityListResult.RowsDTO commodity = new CommodityListResult.RowsDTO(); + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -20,5 +47,80 @@ public class AddCommodityActivity extends AppCompatActivity { v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); + + back = findViewById(R.id.textView22); + nameET = findViewById(R.id.editTextText4); + priceET = findViewById(R.id.editTextNumber); + descriptionET = findViewById(R.id.editTextText3); + spinner = findViewById(R.id.spinner2); + addCommodityBtn = findViewById(R.id.button6); + + SharedPreferences sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + int userId = sharedPreferences.getInt("id", 0); + + commodity.sellerid = userId; + + //设置item的被选择的监听 + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + //当item被选择后调用此方法 + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + //获取我们所选中的内容 + commodity.classification = parent.getItemAtPosition(position).toString(); + } + //只有当patent中的资源没有时,调用此方法 + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + + back.setOnClickListener(v -> { + finish(); + }); + + addCommodityBtn.setOnClickListener(v -> { + // TODO: 提交商品信息 + commodity.name = nameET.getText().toString(); + commodity.price = Double.parseDouble(priceET.getText().toString()); + commodity.description = descriptionET.getText().toString(); + commodity.state = "在售"; + // 获取当前时间的毫秒数 + long currentTimeMillis = System.currentTimeMillis(); + // 将毫秒数转换为Date对象 + Date date = new Date(currentTimeMillis); + // 定义日期格式 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + // 格式化Date对象为字符串 + commodity.time = sdf.format(date); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + Call addCommodityCall = api.addCommodity("Bearer ", commodity); + addCommodityCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show(); + finish(); + }else { + Toast.makeText(getApplicationContext(), "添加失败", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + }); } } \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/Api.java b/src/main/java/com/example/coursedesign/Api.java index 773ee06..d340c30 100644 --- a/src/main/java/com/example/coursedesign/Api.java +++ b/src/main/java/com/example/coursedesign/Api.java @@ -45,4 +45,12 @@ public interface Api { //http://127.0.0.1:8080/system/commodity/list @GET("/system/commodity/list2") Call getCommodityListBySellerId(@Header("Authorization") String token, @Query("sellerid") Integer sellerId, @Query("state") String state); + + //http://127.0.0.1:8080/system/commodity + @PUT("/system/commodity") + Call updateCommodity(@Header("Authorization") String token, @Body CommodityListResult.RowsDTO rowsDTO); + + //http://127.0.0.1:8080/system/commodity + @POST("/system/commodity") + Call addCommodity(@Header("Authorization") String token, @Body CommodityListResult.RowsDTO rowsDTO); } diff --git a/src/main/java/com/example/coursedesign/CommodityActivity.java b/src/main/java/com/example/coursedesign/CommodityActivity.java index 52434d9..3f67105 100644 --- a/src/main/java/com/example/coursedesign/CommodityActivity.java +++ b/src/main/java/com/example/coursedesign/CommodityActivity.java @@ -1,5 +1,7 @@ package com.example.coursedesign; +import static java.security.AccessController.getContext; + import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; @@ -22,8 +24,15 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; +import com.scwang.smart.refresh.footer.ClassicsFooter; +import com.scwang.smart.refresh.header.ClassicsHeader; +import com.scwang.smart.refresh.layout.SmartRefreshLayout; +import com.scwang.smart.refresh.layout.api.RefreshLayout; +import com.scwang.smart.refresh.layout.listener.OnRefreshListener; import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @@ -31,6 +40,8 @@ public class CommodityActivity extends AppCompatActivity { MyAdapter myAdapter; RecyclerView recyclerView; + SmartRefreshLayout smartRefreshLayout; + TextView back; TextView commodityNameTV; TextView priceTV; @@ -123,6 +134,35 @@ public class CommodityActivity extends AppCompatActivity { } }); + //列表刷新 + smartRefreshLayout = findViewById(R.id.smartLayout5); + smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getApplicationContext())); + smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getApplicationContext())); + smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { + @Override + public void onRefresh(@NonNull RefreshLayout refreshLayout) { + + Call commentListCall = api.getCommentListByCommodityId("Bearer ", id); + commentListCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + result = response.body(); + Log.e("result", result.toString()); + if(result.code == 200){ + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + smartRefreshLayout.finishRefresh(500); + } + }); + back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/src/main/java/com/example/coursedesign/MineFragment.java b/src/main/java/com/example/coursedesign/MineFragment.java index 0a24544..899109b 100644 --- a/src/main/java/com/example/coursedesign/MineFragment.java +++ b/src/main/java/com/example/coursedesign/MineFragment.java @@ -1,6 +1,8 @@ package com.example.coursedesign; +import android.app.Activity; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import androidx.fragment.app.Fragment; @@ -9,10 +11,18 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; public class MineFragment extends Fragment { - Button button, button2; + Button logOut; + ImageView myRelease; + ImageView myOrder; + ImageView picture; + TextView name; + TextView phone; + TextView address; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -20,14 +30,46 @@ public class MineFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_mine, container, false); - button = view.findViewById(R.id.button7); - button.setOnClickListener(v -> { - startActivity(new Intent(getActivity(), MyReleaseActivity.class)); + logOut = view.findViewById(R.id.button12); + myOrder = view.findViewById(R.id.imageView18); + myRelease = view.findViewById(R.id.imageView19); + picture = view.findViewById(R.id.imageView16); + name = view.findViewById(R.id.textView41); + phone = view.findViewById(R.id.textView42); + address = view.findViewById(R.id.textView43); + + myOrder.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(getActivity(), MyOrderActivity.class); + startActivity(intent); + } }); - button2 = view.findViewById(R.id.button10); - button2.setOnClickListener(v -> { - startActivity(new Intent(getActivity(), MyOrderActivity.class)); + + myRelease.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + Intent intent = new Intent(getActivity(), MyReleaseActivity.class); + startActivity(intent); + } }); + + logOut.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.clear(); + editor.commit(); + + startActivity(new Intent(getActivity(), LoginActivity.class)); + getActivity().finish(); + } + }); + return view; } } \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/MyOrderActivity.java b/src/main/java/com/example/coursedesign/MyOrderActivity.java index ec61bb7..34fc435 100644 --- a/src/main/java/com/example/coursedesign/MyOrderActivity.java +++ b/src/main/java/com/example/coursedesign/MyOrderActivity.java @@ -22,6 +22,11 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; +import com.scwang.smart.refresh.footer.ClassicsFooter; +import com.scwang.smart.refresh.header.ClassicsHeader; +import com.scwang.smart.refresh.layout.SmartRefreshLayout; +import com.scwang.smart.refresh.layout.api.RefreshLayout; +import com.scwang.smart.refresh.layout.listener.OnRefreshListener; import retrofit2.Call; import retrofit2.Retrofit; @@ -31,6 +36,8 @@ public class MyOrderActivity extends AppCompatActivity { MyAdapter myAdapter; RecyclerView recyclerView; + SmartRefreshLayout smartRefreshLayout; + TextView back; OrderListResult result; @@ -79,6 +86,33 @@ public class MyOrderActivity extends AppCompatActivity { } }); + //列表刷新 + smartRefreshLayout = findViewById(R.id.smartLayout6); + smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getApplicationContext())); + smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getApplicationContext())); + smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { + @Override + public void onRefresh(@NonNull RefreshLayout refreshLayout) { + + Call orderListCall = api.getOrderListByBuyerId("Bearer ", id); + orderListCall.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + result = response.body(); + if (result.code == 200) { + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + smartRefreshLayout.finishRefresh(500); + } + }); + back.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 16e5b07..e704f66 100644 --- a/src/main/java/com/example/coursedesign/NotOnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/NotOnSaleFragment.java @@ -17,8 +17,14 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import com.bumptech.glide.Glide; +import com.scwang.smart.refresh.footer.ClassicsFooter; +import com.scwang.smart.refresh.header.ClassicsHeader; +import com.scwang.smart.refresh.layout.SmartRefreshLayout; +import com.scwang.smart.refresh.layout.api.RefreshLayout; +import com.scwang.smart.refresh.layout.listener.OnRefreshListener; import retrofit2.Call; import retrofit2.Retrofit; @@ -28,6 +34,7 @@ public class NotOnSaleFragment extends Fragment { MyAdapter myAdapter; RecyclerView recyclerView; + SmartRefreshLayout smartRefreshLayout; CommodityListResult result; @@ -69,6 +76,34 @@ public class NotOnSaleFragment extends Fragment { } }); + //列表刷新 + smartRefreshLayout = view.findViewById(R.id.smartLayout4); + smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext())); + smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext())); + smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { + @Override + public void onRefresh(@NonNull RefreshLayout refreshLayout) { + + Call commodityListCall = api.getCommodityListBySellerId("Bearer ", userId, "下架"); + commodityListCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + result = response.body(); + if(result.code == 200){ + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + smartRefreshLayout.finishRefresh(500); + } + }); + return view; } @@ -88,6 +123,48 @@ public class NotOnSaleFragment extends Fragment { holder.commodityPriceTV.setText("¥" + result.rows.get(position).price); holder.descriptionTV.setText(result.rows.get(position).description); Glide.with(getContext()).load(result.rows.get(position).picture).into(holder.commodityPictureIV); + + holder.switchButton.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); + switch (rowsDTO.state){ + case "在售": + rowsDTO.state = "下架"; + break; + case "下架": + rowsDTO.state = "在售"; + break; + } + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + Call updateCommodityCall = api.updateCommodity("Bearer ", rowsDTO); + updateCommodityCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(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 1c046fb..3f4ef09 100644 --- a/src/main/java/com/example/coursedesign/OnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/OnSaleFragment.java @@ -20,8 +20,15 @@ import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; +import com.scwang.smart.refresh.footer.ClassicsFooter; +import com.scwang.smart.refresh.header.ClassicsHeader; +import com.scwang.smart.refresh.layout.SmartRefreshLayout; +import com.scwang.smart.refresh.layout.api.RefreshLayout; +import com.scwang.smart.refresh.layout.listener.OnRefreshListener; import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @@ -29,6 +36,7 @@ public class OnSaleFragment extends Fragment { MyAdapter myAdapter; RecyclerView recyclerView; + SmartRefreshLayout smartRefreshLayout; CommodityListResult result; @@ -70,6 +78,34 @@ public class OnSaleFragment extends Fragment { } }); + //列表刷新 + smartRefreshLayout = view.findViewById(R.id.smartLayout3); + smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext())); + smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext())); + smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { + @Override + public void onRefresh(@NonNull RefreshLayout refreshLayout) { + + Call commodityListCall = api.getCommodityListBySellerId("Bearer ", userId, "在售"); + commodityListCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + result = response.body(); + if(result.code == 200){ + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + smartRefreshLayout.finishRefresh(500); + } + }); + return view; } @@ -89,6 +125,48 @@ public class OnSaleFragment extends Fragment { holder.commodityPriceTV.setText("¥" + result.rows.get(position).price); holder.descriptionTV.setText(result.rows.get(position).description); Glide.with(getContext()).load(result.rows.get(position).picture).into(holder.commodityPictureIV); + + holder.switchButton.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); + switch (rowsDTO.state){ + case "在售": + rowsDTO.state = "下架"; + break; + case "下架": + rowsDTO.state = "在售"; + break; + } + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + Call updateCommodityCall = api.updateCommodity("Bearer ", rowsDTO); + updateCommodityCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(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/res/drawable/a4_1.png b/src/main/res/drawable/a4_1.png new file mode 100644 index 0000000..6237033 Binary files /dev/null and b/src/main/res/drawable/a4_1.png differ diff --git a/src/main/res/drawable/a4_2.png b/src/main/res/drawable/a4_2.png new file mode 100644 index 0000000..75ecab9 Binary files /dev/null and b/src/main/res/drawable/a4_2.png differ diff --git a/src/main/res/drawable/a4_3.png b/src/main/res/drawable/a4_3.png new file mode 100644 index 0000000..21e46de Binary files /dev/null and b/src/main/res/drawable/a4_3.png differ diff --git a/src/main/res/drawable/a4_4.png b/src/main/res/drawable/a4_4.png new file mode 100644 index 0000000..afa2f98 Binary files /dev/null and b/src/main/res/drawable/a4_4.png differ diff --git a/src/main/res/drawable/a4_5.png b/src/main/res/drawable/a4_5.png new file mode 100644 index 0000000..bec965c Binary files /dev/null and b/src/main/res/drawable/a4_5.png differ diff --git a/src/main/res/drawable/a4_6.png b/src/main/res/drawable/a4_6.png new file mode 100644 index 0000000..29505ba Binary files /dev/null and b/src/main/res/drawable/a4_6.png differ diff --git a/src/main/res/layout/activity_add_commodity.xml b/src/main/res/layout/activity_add_commodity.xml index c7f7dbf..3c2dce6 100644 --- a/src/main/res/layout/activity_add_commodity.xml +++ b/src/main/res/layout/activity_add_commodity.xml @@ -52,7 +52,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" - android:layout_marginTop="16dp" + android:layout_marginTop="70dp" android:layout_marginEnd="16dp" android:ems="15" android:hint="描述一下商品信息" @@ -80,7 +80,6 @@ android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:text="价格" - android:textColor="#FFEB3B" android:textSize="20sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="@+id/editTextNumber" @@ -99,6 +98,53 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> + + + + + + + +