This commit is contained in:
YGRXZYGR 2024-12-03 23:55:52 +08:00
parent 08072d6b28
commit 1e2d04778b
5 changed files with 75 additions and 19 deletions

View File

@ -50,6 +50,12 @@ dependencies {
implementation ("io.github.lucksiege:ucrop:v3.11.2")
implementation ("io.github.lucksiege:camerax:v3.11.2")
implementation ("com.github.bumptech.glide:glide:4.15.1")
implementation ("com.squareup.okhttp3:okhttp:4.11.0")
implementation ("com.google.code.gson:gson:2.10.1")
implementation ("com.squareup.okhttp3:logging-interceptor:4.9.3")
implementation ("com.squareup.retrofit2:retrofit:2.9.0")
implementation ("com.squareup.retrofit2:converter-gson:2.9.0")
//权限

View File

@ -0,0 +1,8 @@
package com.hnucm.c202201020328;
import retrofit2.Call;
import retrofit2.http.GET;
import java.util.List;
public interface ApiService {
@GET("test") // 替换为你的 API 路径
Call<List<System_messages>> getSystemMessages();
}

View File

@ -1,5 +1,13 @@
package com.hnucm.c202201020328;
public class System_messages {
public String title;
String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -1,6 +1,6 @@
package com.hnucm.c202201020328;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -16,34 +16,65 @@ import com.gyf.immersionbar.ImmersionBar;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class System_messagesActivity extends AppCompatActivity {
RecyclerView recyclerView1;
MyAdapter myAdapter;
List<System_messages> system_messages=new ArrayList<>();
List<System_messages> system_messages = new ArrayList<>();
ApiService apiService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_system_messages);
ImmersionBar.with(this).init();
recyclerView1=findViewById(R.id.recyclerview1);
for (int i=1;i<=25;i++){
System_messages systemMessages=new System_messages();
String c=String.valueOf(i);
systemMessages.title=c;
system_messages.add(systemMessages);
}
myAdapter=new MyAdapter();
recyclerView1.setAdapter(myAdapter);
recyclerView1 = findViewById(R.id.recyclerview1);
recyclerView1.setLayoutManager(new LinearLayoutManager(this));
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHoler>{
// 初始化 Retrofit ApiService
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://dev.usemock.com/673ff29f94b857f277d90f07/") // 替换为你的 API 基础 URL
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService = retrofit.create(ApiService.class);
// 发起网络请求
fetchSystemMessages();
}
private void fetchSystemMessages() {
Call<List<System_messages>> call = apiService.getSystemMessages();
call.enqueue(new Callback<List<System_messages>>() {
@Override
public void onResponse(Call<List<System_messages>> call, Response<List<System_messages>> response) {
if (response.isSuccessful() && response.body() != null) {
Log.i("test", "Response: " + response.body().toString());
system_messages.clear();
system_messages.addAll(response.body());
myAdapter = new MyAdapter();
recyclerView1.setAdapter(myAdapter);
} else {
Log.e("test", "Response failed: " + response.message());
}
}
@Override
public void onFailure(Call<List<System_messages>> call, Throwable t) {
Log.e("test", "Request failed: " + t.getMessage(), t);
}
});
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHoler> {
@NonNull
@Override
public MyViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(System_messagesActivity.this).inflate(R.layout.item_system_messages,parent,false);
MyViewHoler myViewHoler=new MyViewHoler(view);
return myViewHoler;
View view = LayoutInflater.from(System_messagesActivity.this).inflate(R.layout.item_system_messages, parent, false);
return new MyViewHoler(view);
}
@Override
@ -56,11 +87,13 @@ public class System_messagesActivity extends AppCompatActivity {
return system_messages.size();
}
}
// ViewHolder
public class MyViewHoler extends RecyclerView.ViewHolder {
TextView text;
public MyViewHoler(@NonNull View itemView) {
super(itemView);
text=itemView.findViewById(R.id.textView115);
text = itemView.findViewById(R.id.textView115);
}
}
}

View File

@ -672,7 +672,7 @@
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginTop="50dp"
android:layout_marginTop="84dp"
android:ellipsize="end"
android:paddingLeft="16dp"
android:paddingRight="16dp"
@ -681,6 +681,7 @@
android:textColor="#BBBBBB"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />