5.21

活力值展示:


package com.example.yibaifen;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.ArrayAdapter;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yibaifen.Bean.Student;
import com.example.yibaifen.Dao.Mysqldb;

import java.util.ArrayList;

public class DisplayHuolizhiActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> huolizhiList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_huolizhi);

listView = findViewById(R.id.list_view);
huolizhiList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, huolizhiList);
listView.setAdapter(adapter);

// 调用异步任务获取数据
new FetchHuolizhiTask().execute();
}

private class FetchHuolizhiTask extends AsyncTask<Void, Void, ArrayList<Student>> {
@Override
protected ArrayList<Student> doInBackground(Void... voids) {
Mysqldb mysqldb = new Mysqldb();
ArrayList<Student> students = mysqldb.getAllStudents();
Log.d("DisplayHuolizhiActivity", "Number of students: " + students.size());
return students;
}

@Override
protected void onPostExecute(ArrayList<Student> students) {
if (students != null && !students.isEmpty()) {
for (Student student : students) {
huolizhiList.add("ID: " + student.getId() + ", 活力值: " + student.getHuolizhi());
}
Log.d("DisplayHuolizhiActivity", "Huolizhi list: " + huolizhiList.toString());
adapter.notifyDataSetChanged(); // 更新适配器的数据
} else {
Log.d("DisplayHuolizhiActivity", "No students found or empty list.");
}
}
}
}
posted @ 2024-06-19 22:04  /张根源/  阅读(6)  评论(0编辑  收藏  举报