From 397923fb2ace169c09e8c61d9223e93b3ba26ce7 Mon Sep 17 00:00:00 2001 From: Levi Date: Sat, 30 Nov 2024 23:12:51 +0800 Subject: [PATCH] =?UTF-8?q?Android=E4=BB=A3=E7=A0=8111=E6=9C=8830=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 --- .../com/c202101020117/Action/Action1.java | 2 - .../com/c202101020117/Action/Action2.java | 55 ++++++++++--------- .../com/c202101020117/Action/Action4.java | 44 ++++++++++----- .../com/c202101020117/Action/Action5.java | 50 +++++++++++------ .../com/c202101020117/AppConctroller.java | 26 +++++++++ .../Fragment/DrugListFragment.java | 2 + app/src/main/java/com/c202101020117/Home.java | 15 +++-- .../com/c202101020117/package1/DrugInfo.java | 9 +++ .../com/c202101020117/package1/RYApi.java | 11 ++++ 9 files changed, 151 insertions(+), 63 deletions(-) create mode 100644 app/src/main/java/com/c202101020117/AppConctroller.java create mode 100644 app/src/main/java/com/c202101020117/package1/DrugInfo.java diff --git a/app/src/main/java/com/c202101020117/Action/Action1.java b/app/src/main/java/com/c202101020117/Action/Action1.java index 21cc748..8a55fad 100644 --- a/app/src/main/java/com/c202101020117/Action/Action1.java +++ b/app/src/main/java/com/c202101020117/Action/Action1.java @@ -18,8 +18,6 @@ import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; -import com.c202101020117.package1.Api; -import com.c202101020117.package1.DrugList; import com.c202101020117.R; import com.c202101020117.package1.DrugListResult; import com.c202101020117.package1.RYApi; diff --git a/app/src/main/java/com/c202101020117/Action/Action2.java b/app/src/main/java/com/c202101020117/Action/Action2.java index b5a8736..0e4de4a 100644 --- a/app/src/main/java/com/c202101020117/Action/Action2.java +++ b/app/src/main/java/com/c202101020117/Action/Action2.java @@ -5,6 +5,8 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; + +import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -17,6 +19,8 @@ import com.bumptech.glide.Glide; import com.c202101020117.package1.Api; import com.c202101020117.package1.Drug2List; import com.c202101020117.R; +import com.c202101020117.package1.Drug2ListResult; +import com.c202101020117.package1.RYApi; import com.gyf.immersionbar.ImmersionBar; import com.scwang.smart.refresh.footer.ClassicsFooter; import com.scwang.smart.refresh.header.ClassicsHeader; @@ -36,7 +40,7 @@ public class Action2 extends AppCompatActivity { TextView back; SmartRefreshLayout smartRefreshLayout; - Drug2List drugs; + Drug2ListResult drugs; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,23 +61,27 @@ public class Action2 extends AppCompatActivity { } }); + SharedPreferences sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE); + String token = sharedPreferences.getString("token", null); + //获取数据 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://10.138.63.204:8080/") .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 .build(); - Api api = retrofit.create(Api.class); + RYApi api = retrofit.create(RYApi.class); + + Call drug2ListResultCall = api.getExpiringDrugList("Bearer " + token); + drug2ListResultCall.enqueue(new Callback() { - Call drugListCall = api.getExpiringDrugList(); - drugListCall.enqueue(new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(Call call, Response response) { drugs = response.body(); myAdapter.notifyDataSetChanged(); } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call call, Throwable t) { } }); @@ -85,26 +93,21 @@ public class Action2 extends AppCompatActivity { smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) { -// Retrofit retrofit = new Retrofit.Builder() -// .baseUrl("http://10.138.63.204:8080/") -// .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 -// .build(); -// Api api = retrofit.create(Api.class); - Call drugListCall = api.getExpiringDrugList(); - drugListCall.enqueue(new Callback() { + Call drug2ListResultCall = api.getExpiringDrugList("Bearer " + token); + drug2ListResultCall.enqueue(new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(Call call, Response response) { drugs = response.body(); + smartRefreshLayout.finishRefresh(500); + myAdapter.notifyDataSetChanged(); } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(Call call, Throwable t) { } }); - smartRefreshLayout.finishRefresh(500); - myAdapter.notifyDataSetChanged(); } }); } @@ -124,14 +127,14 @@ public class Action2 extends AppCompatActivity { @Override //每个条目加载一次,该方法就会被调用一次 public void onBindViewHolder(@NonNull MyViewHoler holder, int position) { - holder.DnoTv.setText(drugs.drugList.get(position).dno); - holder.DnameTv.setText(drugs.drugList.get(position).name); - holder.SnoTv.setText(drugs.drugList.get(position).sno); - holder.MFDTv.setText(drugs.drugList.get(position).mfd); - holder.EXPTv.setText(drugs.drugList.get(position).exp); - holder.QuantityTv.setText(drugs.drugList.get(position).quantity+""); - holder.DatediffTv.setText((int)(drugs.drugList.get(position).datediff)+""); - Glide.with(Action2.this).load(drugs.drugList.get(position).picture).into(holder.imageView); + holder.DnoTv.setText(drugs.rows.get(position).dno); + holder.DnameTv.setText(drugs.rows.get(position).name); + holder.SnoTv.setText(drugs.rows.get(position).sno); + holder.MFDTv.setText(drugs.rows.get(position).mFD); + holder.EXPTv.setText(drugs.rows.get(position).eXP); + holder.QuantityTv.setText(drugs.rows.get(position).quantity+""); + holder.DatediffTv.setText((int)(drugs.rows.get(position).datediff)+""); + Glide.with(Action2.this).load(drugs.rows.get(position).picture).into(holder.imageView); holder.constraintLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -154,7 +157,7 @@ public class Action2 extends AppCompatActivity { //用来控制限制列表条数 public int getItemCount() { if(drugs != null) - return drugs.drugList.size(); + return drugs.rows.size(); else return 0; } diff --git a/app/src/main/java/com/c202101020117/Action/Action4.java b/app/src/main/java/com/c202101020117/Action/Action4.java index 8a536ba..6cf0570 100644 --- a/app/src/main/java/com/c202101020117/Action/Action4.java +++ b/app/src/main/java/com/c202101020117/Action/Action4.java @@ -2,23 +2,27 @@ package com.c202101020117.Action; import androidx.appcompat.app.AppCompatActivity; +import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; + import com.c202101020117.R; -import com.c202101020117.package1.Api; +import com.c202101020117.package1.DrugInfo; +import com.c202101020117.package1.RYApi; import com.c202101020117.package1.Result; import com.gyf.immersionbar.ImmersionBar; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; -public class Action4 extends AppCompatActivity { +public class Action4 extends AppCompatActivity { TextView back; EditText text1; @@ -39,36 +43,48 @@ public class Action4 extends AppCompatActivity { text4 = findViewById(R.id.editTextTextPersonName6); confirm = findViewById(R.id.textView78); - SharedPreferences sharedPreferences = getSharedPreferences("action1",MODE_PRIVATE); + SharedPreferences sharedPreferences = getSharedPreferences("action1", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); - Boolean need = sharedPreferences.getBoolean("need",false); + Boolean need = sharedPreferences.getBoolean("need", false); - if(need){ - text1.setText(sharedPreferences.getString("dno","")); + if (need) { + text1.setText(sharedPreferences.getString("dno", "")); editor.clear(); editor.commit(); - }else{ + } else { text1.setText(""); } confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + + SharedPreferences sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE); + String token = sharedPreferences.getString("token", null); + + //获取数据 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://10.138.63.204:8080/") .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 .build(); - Api api = retrofit.create(Api.class); + RYApi api = retrofit.create(RYApi.class); - Call addDrug = api.addDrug(text1.getText()+"",text3.getText()+"",text2.getText()+"",Integer.parseInt(text4.getText()+"")); - addDrug.enqueue(new Callback(){ + DrugInfo drugInfo = new DrugInfo(); + drugInfo.dno = text1.getText() + ""; + drugInfo.EXP = text3.getText() + ""; + drugInfo.MFD = text2.getText() + ""; + drugInfo.quantity = Integer.parseInt(text4.getText() + ""); + + Call addDrug = api.addDrug("Bearer " + token, drugInfo); + addDrug.enqueue(new Callback() { + @Override public void onResponse(Call call, Response response) { Result result = response.body(); - if(result.code==0){ - Toast.makeText(Action4.this,"增添成功",Toast.LENGTH_LONG).show(); + if (result.code == 200) { + Toast.makeText(Action4.this, "增添成功", Toast.LENGTH_LONG).show(); finish(); - }else{ - Toast.makeText(Action4.this,"数据输入错误",Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(Action4.this, "数据输入错误", Toast.LENGTH_LONG).show(); } } diff --git a/app/src/main/java/com/c202101020117/Action/Action5.java b/app/src/main/java/com/c202101020117/Action/Action5.java index 784b6f9..f4cea25 100644 --- a/app/src/main/java/com/c202101020117/Action/Action5.java +++ b/app/src/main/java/com/c202101020117/Action/Action5.java @@ -1,17 +1,24 @@ package com.c202101020117.Action; import androidx.appcompat.app.AppCompatActivity; + +import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; + import com.c202101020117.R; -import com.c202101020117.package1.Api; +import com.c202101020117.package1.DrugInfo; +import com.c202101020117.package1.RYApi; import com.c202101020117.package1.Result; import com.gyf.immersionbar.ImmersionBar; + import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @@ -34,17 +41,17 @@ public class Action5 extends AppCompatActivity { text3 = findViewById(R.id.editTextTextPersonName9); confirm = findViewById(R.id.textView86); - SharedPreferences sharedPreferences = getSharedPreferences("action2",MODE_PRIVATE); + SharedPreferences sharedPreferences = getSharedPreferences("action2", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); - Boolean need = sharedPreferences.getBoolean("need",false); + Boolean need = sharedPreferences.getBoolean("need", false); - if(need){ - text1.setText(sharedPreferences.getString("Dno","")); - text2.setText(sharedPreferences.getString("MFD","")); - text3.setText(sharedPreferences.getString("EXP","")); + if (need) { + text1.setText(sharedPreferences.getString("Dno", "")); + text2.setText(sharedPreferences.getString("MFD", "")); + text3.setText(sharedPreferences.getString("EXP", "")); editor.clear(); editor.commit(); - }else{ + } else { text1.setText(""); text2.setText(""); text3.setText(""); @@ -53,24 +60,35 @@ public class Action5 extends AppCompatActivity { confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + + SharedPreferences sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE); + String token = sharedPreferences.getString("token", null); + + //获取数据 Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://10.138.63.204:8080/") .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 .build(); - Api api = retrofit.create(Api.class); + RYApi api = retrofit.create(RYApi.class); - Call deleteDrug = api.deleteDrug(text1.getText()+"",text3.getText()+"",text2.getText()+""); - deleteDrug.enqueue(new retrofit2.Callback() { + DrugInfo drugInfo = new DrugInfo(); + drugInfo.dno = text1.getText() + ""; + drugInfo.EXP = text3.getText() + ""; + drugInfo.MFD = text2.getText() + ""; + + Call deleteDrug = api.deleteDrug("Bearer " + token, drugInfo); + deleteDrug.enqueue(new Callback() { @Override - public void onResponse(Call call, retrofit2.Response response) { + public void onResponse(Call call, Response response) { Result result = response.body(); - if(result.code==0){ - Toast.makeText(Action5.this,"删除成功",Toast.LENGTH_LONG).show(); + if (result.code == 200) { + Toast.makeText(Action5.this, "删除成功", Toast.LENGTH_LONG).show(); finish(); - }else{ - Toast.makeText(Action5.this,"数据输入错误",Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(Action5.this, "数据输入错误", Toast.LENGTH_LONG).show(); } } + @Override public void onFailure(Call call, Throwable t) { diff --git a/app/src/main/java/com/c202101020117/AppConctroller.java b/app/src/main/java/com/c202101020117/AppConctroller.java new file mode 100644 index 0000000..d79b8df --- /dev/null +++ b/app/src/main/java/com/c202101020117/AppConctroller.java @@ -0,0 +1,26 @@ +package com.c202101020117; + +import android.app.Activity; +import android.app.Application; + +import java.util.ArrayList; +import java.util.List; + +public class AppConctroller extends Application { + public static List lists = new ArrayList<>(); + + public static void addActivity(Activity activity) { + lists.add(activity); + } + + public static void removeActivity(Activity activity) { + lists.remove(activity); + } + + public static void exit() { + for (Activity activity : lists) { + activity.finish(); + } + lists.clear(); + } +} diff --git a/app/src/main/java/com/c202101020117/Fragment/DrugListFragment.java b/app/src/main/java/com/c202101020117/Fragment/DrugListFragment.java index 0b46670..3833630 100644 --- a/app/src/main/java/com/c202101020117/Fragment/DrugListFragment.java +++ b/app/src/main/java/com/c202101020117/Fragment/DrugListFragment.java @@ -17,9 +17,11 @@ import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import com.bumptech.glide.Glide; import com.c202101020117.Action.DetailedInformation; +import com.c202101020117.Login; import com.c202101020117.R; import com.c202101020117.package1.DrugListResult; import com.c202101020117.package1.RYApi; diff --git a/app/src/main/java/com/c202101020117/Home.java b/app/src/main/java/com/c202101020117/Home.java index 0304d24..c20dd17 100644 --- a/app/src/main/java/com/c202101020117/Home.java +++ b/app/src/main/java/com/c202101020117/Home.java @@ -2,8 +2,11 @@ package com.c202101020117; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; + import android.os.Bundle; import android.view.View; +import android.widget.Toast; + import com.c202101020117.Fragment.HomeFragment; import com.c202101020117.Fragment.DrugListFragment; import com.c202101020117.Fragment.ScreeningFragment; @@ -27,14 +30,17 @@ public class Home extends AppCompatActivity { setContentView(R.layout.activity_home); ImmersionBar.with(this).init(); + AppConctroller.addActivity(this); + Toast.makeText(this, AppConctroller.lists.size()+"", Toast.LENGTH_LONG).show(); + homeFragment = new HomeFragment(); drugListFragment = new DrugListFragment(); screeningFragment = new ScreeningFragment(); updateFragment = new UpdateFragment(); - getSupportFragmentManager().beginTransaction().add(R.id.fragment,homeFragment).commit(); - getSupportFragmentManager().beginTransaction().add(R.id.fragment,drugListFragment).commit(); - getSupportFragmentManager().beginTransaction().add(R.id.fragment,screeningFragment).commit(); - getSupportFragmentManager().beginTransaction().add(R.id.fragment,updateFragment).commit(); + getSupportFragmentManager().beginTransaction().add(R.id.fragment, homeFragment).commit(); + getSupportFragmentManager().beginTransaction().add(R.id.fragment, drugListFragment).commit(); + getSupportFragmentManager().beginTransaction().add(R.id.fragment, screeningFragment).commit(); + getSupportFragmentManager().beginTransaction().add(R.id.fragment, updateFragment).commit(); getSupportFragmentManager().beginTransaction() .show(homeFragment) .hide(drugListFragment) @@ -114,6 +120,5 @@ public class Home extends AppCompatActivity { .commit(); } }); - } } \ No newline at end of file diff --git a/app/src/main/java/com/c202101020117/package1/DrugInfo.java b/app/src/main/java/com/c202101020117/package1/DrugInfo.java new file mode 100644 index 0000000..8b9b746 --- /dev/null +++ b/app/src/main/java/com/c202101020117/package1/DrugInfo.java @@ -0,0 +1,9 @@ +package com.c202101020117.package1; + +public class DrugInfo { + public Integer id; + public String dno; + public String EXP; + public String MFD; + public Integer quantity; +} diff --git a/app/src/main/java/com/c202101020117/package1/RYApi.java b/app/src/main/java/com/c202101020117/package1/RYApi.java index 9860626..5cf5347 100644 --- a/app/src/main/java/com/c202101020117/package1/RYApi.java +++ b/app/src/main/java/com/c202101020117/package1/RYApi.java @@ -1,12 +1,15 @@ package com.c202101020117.package1; +import java.util.Date; 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.Query; public interface RYApi { @@ -35,6 +38,14 @@ public interface RYApi { Call getUndersupplyDrugList(@Header("Authorization") String token); //http://127.0.0.1:8080/system/adrug/expiringlist + @GET("/system/adrug/expiringlist") + Call getExpiringDrugList(@Header("Authorization") String token); + //http://127.0.0.1:8080/system/quantity + @POST("/system/quantity") + Call addDrug(@Header("Authorization") String token, @Body DrugInfo drugInfo); + //http://127.0.0.1:8080/system/adrug/remove + @POST("/system/adrug/remove") + Call deleteDrug(@Header("Authorization") String token, @Body DrugInfo drugInfo); }