diff --git a/build.gradle.kts b/build.gradle.kts index 4dcf4fb..caa6dbd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,6 +42,11 @@ dependencies { androidTestImplementation(libs.espresso.core) implementation ("de.hdodenhof:circleimageview:3.1.0") + implementation ("com.squareup.okhttp3:okhttp:4.11.0") + implementation ("com.squareup.retrofit2:retrofit:2.9.0") + implementation ("com.squareup.retrofit2:converter-gson:2.9.0") // 如果你需要使用 Gson 转换器 + implementation ("com.google.code.gson:gson:2.10.1") + implementation ("com.squareup.okhttp3:logging-interceptor:4.9.3") // 基础依赖包,必须要依赖 implementation ("com.geyifeng.immersionbar:immersionbar:3.2.2") diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 923a511..3fe361c 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -2,7 +2,10 @@ + + + diff --git a/src/main/java/com/example/coursedesign/Api.java b/src/main/java/com/example/coursedesign/Api.java new file mode 100644 index 0000000..b1769a0 --- /dev/null +++ b/src/main/java/com/example/coursedesign/Api.java @@ -0,0 +1,44 @@ +package com.example.coursedesign; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.Map; + +import retrofit2.Call; +import retrofit2.http.Body; +import retrofit2.http.GET; +import retrofit2.http.Header; +import retrofit2.http.POST; +import retrofit2.http.PUT; +import retrofit2.http.Query; + +public interface Api { + + //http://127.0.0.1:8080/system/person/login + @GET("/system/person/login") + Call login(@Header("Authorization") String token, @Query("phone") String username, @Query("password") String password); + + //http://127.0.0.1:8080/system/commodity/list + @GET("/system/commodity/list2") + Call getCommodityList(@Header("Authorization") String token); + + //http://127.0.0.1:8080/system/commodity/list + @GET("/system/commodity/list2") + Call getCommodityListById(@Header("Authorization") String token, @Query("id") Integer id); + + //http://127.0.0.1:8080/system/commodity/list + @GET("/system/commodity/list2") + Call getCommodityListByNameOrClassification(@Header("Authorization") String token, @Query("name") String name, @Query("classification") String classification); + + //http://127.0.0.1:8080/system/comment/list2 + @GET("/system/comment/list2") + Call getCommentListByCommodityId(@Header("Authorization") String token, @Query("commodityid") Integer commodityId); + + //http://127.0.0.1:8080/system/favorite/list2 + @GET("/system/favorite/list2") + Call getFavoriteListByPersonId(@Header("Authorization") String token, @Query("personid") Integer personId); + + //http://127.0.0.1:8080/system/order/list2 + @GET("/system/order/list2") + Call getOrderListByBuyerId(@Header("Authorization") String token, @Query("buyerid") Integer buyerId); +} diff --git a/src/main/java/com/example/coursedesign/CommentListResult.java b/src/main/java/com/example/coursedesign/CommentListResult.java new file mode 100644 index 0000000..88279c1 --- /dev/null +++ b/src/main/java/com/example/coursedesign/CommentListResult.java @@ -0,0 +1,28 @@ +package com.example.coursedesign; + +import java.util.List; + +public class CommentListResult { + + public Integer total; + public List rows; + public Integer code; + public String msg; + + public static class RowsDTO { + public Object createBy; + public Object createTime; + public Object updateBy; + public Object updateTime; + public Object remark; + public Integer id; + public Integer personid; + public String personname; + public Object personpicture; + public Integer commodityid; + public String commodityname; + public Object commoditypicture; + public String content; + public String time; + } +} diff --git a/src/main/java/com/example/coursedesign/CommodityActivity.java b/src/main/java/com/example/coursedesign/CommodityActivity.java index 9bd631d..c9b84e9 100644 --- a/src/main/java/com/example/coursedesign/CommodityActivity.java +++ b/src/main/java/com/example/coursedesign/CommodityActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -22,10 +23,25 @@ import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; +import retrofit2.Call; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + public class CommodityActivity extends AppCompatActivity { MyAdapter myAdapter; RecyclerView recyclerView; + TextView back; + TextView commodityNameTV; + TextView priceTV; + TextView timeTV; + TextView classificationTV; + ImageView commodityImage; + TextView descriptionTV; + TextView sellerNameTV; + ImageView sellerImage; + + CommentListResult result; @Override protected void onCreate(Bundle savedInstanceState) { @@ -42,6 +58,77 @@ public class CommodityActivity extends AppCompatActivity { myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(CommodityActivity.this)); + + back = findViewById(R.id.textView13); + sellerNameTV = findViewById(R.id.textView14); + priceTV = findViewById(R.id.textView15); + commodityNameTV = findViewById(R.id.textView12); + descriptionTV = findViewById(R.id.textView17); + timeTV = findViewById(R.id.textView16); + classificationTV = findViewById(R.id.textView34); + sellerImage = findViewById(R.id.imageView10); + commodityImage = findViewById(R.id.imageView9); + + SharedPreferences sharedPreferences = getSharedPreferences("commodity", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + int id = sharedPreferences.getInt("id", 0); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + Call commodityListCall = api.getCommodityListById("Bearer ", id); + commodityListCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + CommodityListResult result = response.body(); + if(result.code == 200){ + CommodityListResult.RowsDTO rowsDTO = result.rows.get(0); + commodityNameTV.setText(rowsDTO.name); + priceTV.setText("¥" + rowsDTO.price); + descriptionTV.setText(rowsDTO.description); + timeTV.setText(rowsDTO.time); + classificationTV.setText(rowsDTO.classification); + sellerNameTV.setText(rowsDTO.sellername); + Glide.with(getApplicationContext()).load(rowsDTO.picture).into(commodityImage); + Glide.with(getApplicationContext()).load(rowsDTO.sellerpicture).into(sellerImage); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + + 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) { + + } + }); + + back.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); } public class MyAdapter extends RecyclerView.Adapter { @@ -57,18 +144,34 @@ public class CommodityActivity extends AppCompatActivity { @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { + holder.personNameTV.setText(result.rows.get(position).personname); + holder.contentTV.setText(result.rows.get(position).content); + Glide.with(getApplicationContext()).load(result.rows.get(position).personpicture).into(holder.personImage); } @Override public int getItemCount() { - return 4; + if(result != null){ + return result.rows.size(); + }else{ + return 0; + } } } public class MyViewHolder extends RecyclerView.ViewHolder { + TextView personNameTV; + TextView contentTV; + ImageView personImage; + ConstraintLayout constraintLayout; + public MyViewHolder(@NonNull View itemView) { super(itemView); + personNameTV = itemView.findViewById(R.id.textView19); + contentTV = itemView.findViewById(R.id.textView20); + personImage = itemView.findViewById(R.id.imageView12); + constraintLayout = itemView.findViewById(R.id.constraintLayout2); } } } \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/CommodityListResult.java b/src/main/java/com/example/coursedesign/CommodityListResult.java new file mode 100644 index 0000000..6e245d4 --- /dev/null +++ b/src/main/java/com/example/coursedesign/CommodityListResult.java @@ -0,0 +1,30 @@ +package com.example.coursedesign; + +import java.util.List; + +public class CommodityListResult { + + public Integer total; + public List rows; + public Integer code; + public String msg; + + public static class RowsDTO { + public Object createBy; + public Object createTime; + public Object updateBy; + public Object updateTime; + public Object remark; + public Integer id; + public String name; + public Integer sellerid; + public String sellername; + public Object sellerpicture; + public Double price; + public String classification; + public String description; + public Object picture; + public String state; + public String time; + } +} diff --git a/src/main/java/com/example/coursedesign/FavoriteFragment.java b/src/main/java/com/example/coursedesign/FavoriteFragment.java index 53bbb59..7738793 100644 --- a/src/main/java/com/example/coursedesign/FavoriteFragment.java +++ b/src/main/java/com/example/coursedesign/FavoriteFragment.java @@ -11,18 +11,33 @@ import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; 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.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; public class FavoriteFragment extends Fragment { MyAdapter myAdapter; RecyclerView recyclerView; + SmartRefreshLayout smartRefreshLayout; + FavoriteListResult result; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -30,11 +45,62 @@ public class FavoriteFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_favorite, container, false); - recyclerView = view.findViewById(R.id.recycleView2); + recyclerView = view.findViewById(R.id.recyclerView2); myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + Call favoriteListCall = api.getFavoriteListByPersonId("Bearer ", 1); + favoriteListCall.enqueue(new 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 = view.findViewById(R.id.smartLayout2); + smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext())); + smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext())); + smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { + @Override + public void onRefresh(@NonNull RefreshLayout refreshLayout) { + Call favoriteListCall = api.getFavoriteListByPersonId("Bearer ", 1); + favoriteListCall.enqueue(new 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; } @@ -51,20 +117,63 @@ public class FavoriteFragment extends Fragment { @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { + holder.commodityNameTV.setText(result.rows.get(position).commodityname); + holder.priceTV.setText("¥" + result.rows.get(position).price); + holder.descriptionTV.setText(result.rows.get(position).description); + holder.sellerNameTV.setText(result.rows.get(position).sellername); + holder.timeTV.setText(result.rows.get(position).time); + Glide.with(getContext()).load(result.rows.get(position).commoditypicture).into(holder.commodityImage); + Glide.with(getContext()).load(result.rows.get(position).sellerpicture).into(holder.sellerImage); + + holder.constraintLayout.setOnClickListener(new View.OnClickListener(){ + + @Override + public void onClick(View v) { + + int adapterPosition = holder.getAdapterPosition(); + if(adapterPosition != RecyclerView.NO_POSITION){ + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("commodity", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.clear(); + editor.putInt("id", result.rows.get(adapterPosition).id); + editor.commit(); + startActivity(new Intent(getActivity(), CommodityActivity.class)); + } + } + }); } @Override public int getItemCount() { - return 4; + if(result != null){ + return result.rows.size(); + }else{ + return 0; + } } } public class MyViewHolder extends RecyclerView.ViewHolder { + TextView commodityNameTV; + TextView priceTV; + ImageView commodityImage; + TextView descriptionTV; + TextView sellerNameTV; + ImageView sellerImage; + TextView timeTV; + ConstraintLayout constraintLayout; public MyViewHolder(@NonNull View itemView) { super(itemView); - + commodityNameTV = itemView.findViewById(R.id.textView8); + priceTV = itemView.findViewById(R.id.textView9); + commodityImage = itemView.findViewById(R.id.imageView7); + descriptionTV = itemView.findViewById(R.id.textView18); + sellerNameTV = itemView.findViewById(R.id.textView10); + sellerImage = itemView.findViewById(R.id.imageView8); + timeTV = itemView.findViewById(R.id.textView25); + constraintLayout = itemView.findViewById(R.id.commodityLayout); } } } \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/FavoriteListResult.java b/src/main/java/com/example/coursedesign/FavoriteListResult.java new file mode 100644 index 0000000..c7c2951 --- /dev/null +++ b/src/main/java/com/example/coursedesign/FavoriteListResult.java @@ -0,0 +1,35 @@ +package com.example.coursedesign; + +import java.util.List; + +public class FavoriteListResult { + + + public Integer total; + public List rows; + public Integer code; + public String msg; + + public static class RowsDTO { + public Object createBy; + public Object createTime; + public Object updateBy; + public Object updateTime; + public Object remark; + public Integer id; + public Integer personid; + public String personname; + public String personpicture; + public Integer commodityid; + public String commodityname; + public String commoditypicture; + public Integer sellerid; + public String sellername; + public String sellerpicture; + public Double price; + public String classification; + public String description; + public String state; + public String time; + } +} diff --git a/src/main/java/com/example/coursedesign/HomeFragment.java b/src/main/java/com/example/coursedesign/HomeFragment.java index d8369cb..8cb7ede 100644 --- a/src/main/java/com/example/coursedesign/HomeFragment.java +++ b/src/main/java/com/example/coursedesign/HomeFragment.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.annotation.NonNull; @@ -9,14 +11,40 @@ import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.Spinner; +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; public class HomeFragment extends Fragment { MyAdapter myAdapter; RecyclerView recyclerView; + SmartRefreshLayout smartRefreshLayout; + CommodityListResult result; + + Spinner spinner; + EditText search; + ImageView searchImage; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -24,11 +52,107 @@ public class HomeFragment extends Fragment { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_home, container, false); - recyclerView = view.findViewById(R.id.recycleView); + recyclerView = view.findViewById(R.id.recyclerView); myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); + spinner = view.findViewById(R.id.spinner); + search = view.findViewById(R.id.editTextText2); + searchImage = view.findViewById(R.id.imageView6); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + //设置item的被选择的监听 + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + //当item被选择后调用此方法 + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + //获取我们所选中的内容 + String classcification = parent.getItemAtPosition(position).toString(); + + if(classcification.equals("全部")){ + Call commodityListCall = api.getCommodityList("Bearer "); + commodityListCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, Response response) { + result = response.body(); + if(result.code == 200){ + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + }else{ + Call commodityListCall = api.getCommodityListByNameOrClassification("Bearer ", null, classcification); + commodityListCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, Response response) { + result = response.body(); + if(result.code == 200){ + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + } + } + //只有当patent中的资源没有时,调用此方法 + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + + //列表刷新 + smartRefreshLayout = view.findViewById(R.id.smartLayout); + smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext())); + smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext())); + smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { + @Override + public void onRefresh(@NonNull RefreshLayout refreshLayout) { + search.setText(""); + spinner.setSelection(0); + smartRefreshLayout.finishRefresh(500); + } + }); + + searchImage.setOnClickListener(v -> { + + spinner.setSelection(0); + Call commodityListCall = api.getCommodityListByNameOrClassification("Bearer ", search.getText().toString(), null); + commodityListCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, Response response) { + result = response.body(); + if(result.code == 200){ + myAdapter.notifyDataSetChanged(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + }); + return view; } @@ -45,28 +169,62 @@ public class HomeFragment extends Fragment { @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { + holder.commodityNameTV.setText(result.rows.get(position).name); + holder.priceTV.setText("¥" + result.rows.get(position).price); + holder.descriptionTV.setText(result.rows.get(position).description); + holder.sellerNameTV.setText(result.rows.get(position).sellername); + holder.timeTV.setText(result.rows.get(position).time); + Glide.with(getContext()).load(result.rows.get(position).picture).into(holder.commodityImage); + Glide.with(getContext()).load(result.rows.get(position).sellerpicture).into(holder.sellerImage); + holder.constraintLayout.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { - Intent intent = new Intent(getActivity(), CommodityActivity.class); - startActivity(intent); + + int adapterPosition = holder.getAdapterPosition(); + if(adapterPosition != RecyclerView.NO_POSITION){ + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("commodity", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.clear(); + editor.putInt("id", result.rows.get(adapterPosition).id); + editor.commit(); + startActivity(new Intent(getActivity(), CommodityActivity.class)); + } } }); } @Override public int getItemCount() { - return 10; + if(result != null){ + return result.rows.size(); + }else{ + return 0; + } } } public class MyViewHolder extends RecyclerView.ViewHolder { + TextView commodityNameTV; + TextView priceTV; + ImageView commodityImage; + TextView descriptionTV; + TextView sellerNameTV; + ImageView sellerImage; + TextView timeTV; ConstraintLayout constraintLayout; public MyViewHolder(@NonNull View itemView) { super(itemView); + commodityNameTV = itemView.findViewById(R.id.textView8); + priceTV = itemView.findViewById(R.id.textView9); + commodityImage = itemView.findViewById(R.id.imageView7); + descriptionTV = itemView.findViewById(R.id.textView18); + sellerNameTV = itemView.findViewById(R.id.textView10); + sellerImage = itemView.findViewById(R.id.imageView8); + timeTV = itemView.findViewById(R.id.textView25); constraintLayout = itemView.findViewById(R.id.commodityLayout); } } diff --git a/src/main/java/com/example/coursedesign/LoginActivity.java b/src/main/java/com/example/coursedesign/LoginActivity.java index 280559e..572cfa3 100644 --- a/src/main/java/com/example/coursedesign/LoginActivity.java +++ b/src/main/java/com/example/coursedesign/LoginActivity.java @@ -1,9 +1,12 @@ package com.example.coursedesign; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.widget.Button; import android.widget.EditText; +import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; @@ -11,10 +14,19 @@ 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; +import retrofit2.converter.gson.GsonConverterFactory; + public class LoginActivity extends AppCompatActivity { EditText phone, password; Button login, register; + LoginResult result; @Override protected void onCreate(Bundle savedInstanceState) { @@ -32,9 +44,48 @@ public class LoginActivity extends AppCompatActivity { login = findViewById(R.id.button2); register = findViewById(R.id.button); + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + //登录按钮监听 login.setOnClickListener(v -> { + + Call loginResultCall = api.login("Bearer ", phone.getText().toString(), password.getText().toString()); + startActivity(new Intent(LoginActivity.this, HomeActivity.class)); + loginResultCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + result = response.body(); + if (result != null && result.data != null) { + SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.clear(); +// editor.putString("token", loginResult.token); + editor.putBoolean("isLogin", true); + editor.putInt("id", result.data.id); + editor.putString("phone", result.data.phone); + editor.putString("name", result.data.name); + editor.putString("picture", result.data.picture); + editor.commit(); + + Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show(); + startActivity(new Intent(LoginActivity.this, HomeActivity.class)); + finish(); + } else { + Toast.makeText(LoginActivity.this, "账号或者密码错误", Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); }); //注册按钮监听 diff --git a/src/main/java/com/example/coursedesign/LoginResult.java b/src/main/java/com/example/coursedesign/LoginResult.java new file mode 100644 index 0000000..33a9bbd --- /dev/null +++ b/src/main/java/com/example/coursedesign/LoginResult.java @@ -0,0 +1,25 @@ +package com.example.coursedesign; + +public class LoginResult { + + public String msg; + public Integer code; + public DataDTO data; + + public static class DataDTO { + public Object createBy; + public Object createTime; + public Object updateBy; + public Object updateTime; + public Object remark; + public Integer id; + public String name; + public String picture; + public Object gender; + public Object age; + public String phone; + public Object password; + public Object address; + public Object major; + } +} diff --git a/src/main/java/com/example/coursedesign/MineFragment.java b/src/main/java/com/example/coursedesign/MineFragment.java index b31776c..0a24544 100644 --- a/src/main/java/com/example/coursedesign/MineFragment.java +++ b/src/main/java/com/example/coursedesign/MineFragment.java @@ -12,7 +12,7 @@ import android.widget.Button; public class MineFragment extends Fragment { - Button button; + Button button, button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -24,6 +24,10 @@ public class MineFragment extends Fragment { button.setOnClickListener(v -> { startActivity(new Intent(getActivity(), MyReleaseActivity.class)); }); + button2 = view.findViewById(R.id.button10); + button2.setOnClickListener(v -> { + startActivity(new Intent(getActivity(), MyOrderActivity.class)); + }); 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 new file mode 100644 index 0000000..ec61bb7 --- /dev/null +++ b/src/main/java/com/example/coursedesign/MyOrderActivity.java @@ -0,0 +1,160 @@ +package com.example.coursedesign; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.activity.EdgeToEdge; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.bumptech.glide.Glide; + +import retrofit2.Call; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +public class MyOrderActivity extends AppCompatActivity { + + MyAdapter myAdapter; + RecyclerView recyclerView; + TextView back; + OrderListResult result; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_my_order); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + recyclerView = findViewById(R.id.recyclerView5); + myAdapter = new MyAdapter(); + recyclerView.setAdapter(myAdapter); + recyclerView.setLayoutManager(new LinearLayoutManager(MyOrderActivity.this)); + + back = findViewById(R.id.textView33); + + SharedPreferences sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + int id = sharedPreferences.getInt("id", 0); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + 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()); + } + }); + + back.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); + } + + public class MyAdapter extends RecyclerView.Adapter { + + @NonNull + @Override + public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(MyOrderActivity.this).inflate(R.layout.commodity_item_layout, parent, false); + MyViewHolder myViewHolder = new MyViewHolder(view); + return myViewHolder; + } + + @Override + public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { + + holder.commodityNameTV.setText(result.rows.get(position).commodityname); + holder.priceTV.setText("¥" + result.rows.get(position).price); + holder.descriptionTV.setText(result.rows.get(position).description); + holder.sellerNameTV.setText(result.rows.get(position).sellername); + holder.timeTV.setText(result.rows.get(position).time); + Glide.with(getApplicationContext()).load(result.rows.get(position).commoditypicture).into(holder.commodityImage); + Glide.with(getApplicationContext()).load(result.rows.get(position).sellerpicture).into(holder.sellerImage); + + holder.constraintLayout.setOnClickListener(new View.OnClickListener(){ + + @Override + public void onClick(View v) { + int adapterPosition = holder.getAdapterPosition(); + if(adapterPosition != RecyclerView.NO_POSITION){ + SharedPreferences sharedPreferences = getSharedPreferences("commodity", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.clear(); + editor.putInt("id", result.rows.get(adapterPosition).commodityid); + editor.commit(); + startActivity(new Intent(MyOrderActivity.this, CommodityActivity.class)); + } + } + }); + } + + @Override + public int getItemCount() { + if(result != null){ + return result.rows.size(); + }else{ + return 0; + } + } + } + + public class MyViewHolder extends RecyclerView.ViewHolder { + TextView commodityNameTV; + TextView priceTV; + ImageView commodityImage; + TextView descriptionTV; + TextView sellerNameTV; + ImageView sellerImage; + TextView timeTV; + ConstraintLayout constraintLayout; + + public MyViewHolder(@NonNull View itemView) { + super(itemView); + commodityNameTV = itemView.findViewById(R.id.textView8); + priceTV = itemView.findViewById(R.id.textView9); + commodityImage = itemView.findViewById(R.id.imageView7); + descriptionTV = itemView.findViewById(R.id.textView18); + sellerNameTV = itemView.findViewById(R.id.textView10); + sellerImage = itemView.findViewById(R.id.imageView8); + timeTV = itemView.findViewById(R.id.textView25); + constraintLayout = itemView.findViewById(R.id.commodityLayout); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/NotOnSaleFragment.java b/src/main/java/com/example/coursedesign/NotOnSaleFragment.java index 4469078..5edc458 100644 --- a/src/main/java/com/example/coursedesign/NotOnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/NotOnSaleFragment.java @@ -22,7 +22,7 @@ public class NotOnSaleFragment extends Fragment { Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_not_on_sale, container, false); - recyclerView = view.findViewById(R.id.recycleView4); + recyclerView = view.findViewById(R.id.recyclerView4); myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); diff --git a/src/main/java/com/example/coursedesign/OnSaleFragment.java b/src/main/java/com/example/coursedesign/OnSaleFragment.java index c08eadc..caf5ed4 100644 --- a/src/main/java/com/example/coursedesign/OnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/OnSaleFragment.java @@ -1,8 +1,5 @@ package com.example.coursedesign; -import android.app.Activity; -import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import androidx.annotation.NonNull; @@ -14,11 +11,10 @@ import androidx.recyclerview.widget.RecyclerView; 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 com.bumptech.glide.Glide; - public class OnSaleFragment extends Fragment { MyAdapter myAdapter; @@ -29,7 +25,7 @@ public class OnSaleFragment extends Fragment { Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_on_sale, container, false); - recyclerView = view.findViewById(R.id.recycleView3); + recyclerView = view.findViewById(R.id.recyclerView6); myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); @@ -58,11 +54,24 @@ public class OnSaleFragment extends Fragment { } public class MyViewHolder extends RecyclerView.ViewHolder { + + TextView commodityNameTV; + TextView commodityPriceTV; + TextView descriptionTV; + Button switchButton; + Button deleteButton; + ImageView commodityPictureIV; ConstraintLayout constraintLayout; public MyViewHolder(@NonNull View itemView) { super(itemView); - + commodityNameTV = itemView.findViewById(R.id.textView29); + commodityPriceTV = itemView.findViewById(R.id.textView30); + descriptionTV = itemView.findViewById(R.id.textView31); + switchButton = itemView.findViewById(R.id.button9); + deleteButton = itemView.findViewById(R.id.button8); + commodityPictureIV = itemView.findViewById(R.id.imageView14); + constraintLayout = itemView.findViewById(R.id.constraintLayout3); } } } \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/OrderListResult.java b/src/main/java/com/example/coursedesign/OrderListResult.java new file mode 100644 index 0000000..03583ca --- /dev/null +++ b/src/main/java/com/example/coursedesign/OrderListResult.java @@ -0,0 +1,35 @@ +package com.example.coursedesign; + +import java.util.List; + +public class OrderListResult { + + public Integer total; + public List rows; + public Integer code; + public String msg; + + public static class RowsDTO { + public Object createBy; + public Object createTime; + public Object updateBy; + public Object updateTime; + public Object remark; + public Integer id; + public Integer buyerid; + public String buyername; + public String buyerpicture; + public Integer commodityid; + public String commodityname; + public String commoditypicture; + public Integer sellerid; + public String sellername; + public String sellerpicture; + public Double price; + public String classification; + public String description; + public String state; + public String time; + public String status; + } +} diff --git a/src/main/java/com/example/coursedesign/RegistrationActivity.java b/src/main/java/com/example/coursedesign/RegistrationActivity.java index 0ff3d24..4172c30 100644 --- a/src/main/java/com/example/coursedesign/RegistrationActivity.java +++ b/src/main/java/com/example/coursedesign/RegistrationActivity.java @@ -13,7 +13,7 @@ import androidx.core.view.WindowInsetsCompat; public class RegistrationActivity extends AppCompatActivity { EditText username,phone,password; - Button register; + Button register, back; @Override protected void onCreate(Bundle savedInstanceState) { @@ -30,10 +30,15 @@ public class RegistrationActivity extends AppCompatActivity { phone = findViewById(R.id.editTextPhone); password = findViewById(R.id.editTextTextPassword); register = findViewById(R.id.button3); + back = findViewById(R.id.button11); // 注册按钮监听 register.setOnClickListener(v -> { }); + + back.setOnClickListener(v -> { + finish(); + }); } } \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/Result.java b/src/main/java/com/example/coursedesign/Result.java new file mode 100644 index 0000000..1a2720c --- /dev/null +++ b/src/main/java/com/example/coursedesign/Result.java @@ -0,0 +1,6 @@ +package com.example.coursedesign; + +public class Result { + public String msg; + public Integer code; +} diff --git a/src/main/java/com/example/coursedesign/SoldOutFragment.java b/src/main/java/com/example/coursedesign/SoldOutFragment.java index f7e539f..4eea7c6 100644 --- a/src/main/java/com/example/coursedesign/SoldOutFragment.java +++ b/src/main/java/com/example/coursedesign/SoldOutFragment.java @@ -22,7 +22,7 @@ public class SoldOutFragment extends Fragment { Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_sold_out, container, false); - recyclerView = view.findViewById(R.id.recycleView4); + recyclerView = view.findViewById(R.id.recyclerView4); myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); diff --git a/src/main/res/layout/activity_commodity.xml b/src/main/res/layout/activity_commodity.xml index f2ea9c0..9555203 100644 --- a/src/main/res/layout/activity_commodity.xml +++ b/src/main/res/layout/activity_commodity.xml @@ -25,14 +25,15 @@ android:id="@+id/textView13" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginStart="24dp" + android:layout_marginStart="16dp" android:text="Back" android:textColor="@color/teal_200" android:textSize="24sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="@+id/textView12" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="@+id/textView12" /> + app:layout_constraintTop_toTopOf="@+id/textView12" + app:layout_constraintVertical_bias="0.0" /> + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/activity_registration.xml b/src/main/res/layout/activity_registration.xml index 9b2018d..92adfff 100644 --- a/src/main/res/layout/activity_registration.xml +++ b/src/main/res/layout/activity_registration.xml @@ -90,9 +90,24 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" + android:layout_marginEnd="24dp" android:backgroundTint="#5FEDFF" android:text="注册" app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.5" + app:layout_constraintStart_toEndOf="@+id/button11" + app:layout_constraintTop_toBottomOf="@+id/editTextTextPassword" /> + +