package com.example.ourhomework1.activities;标签:searchWhat,ourhomework1,12.28,new,import,android,com From: https://www.cnblogs.com/yang-----/p/18658672
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.ourhomework1.R;
import com.example.ourhomework1.dao.UserDao;
import com.example.ourhomework1.pojo.Records;
import com.example.ourhomework1.pojo.Student;
import com.example.ourhomework1.utils.CommonUtils;
import java.util.ArrayList;
import java.util.List;
public class tDailyRecordActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_searchWhat;
private Button btn_search;
private ListView lv_dailyRecords;
private UserDao userDao;
private Handler mainHandler;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tdaily_record);
userDao = new UserDao();
et_searchWhat = findViewById(R.id.et_searchWhat);
btn_search = findViewById(R.id.btn_search);
lv_dailyRecords = findViewById(R.id.lv_dailyRecords);
mainHandler = new Handler(getMainLooper());
btn_search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_search) {
String searchWhat = et_searchWhat.getText().toString().trim();
new Thread(new Runnable() {
@Override
public void run() {
List<Records> records = userDao.getDailyRecords(searchWhat);
List<String> list = new ArrayList<>();
mainHandler.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < records.size(); i++) {
list.add(records.get(i).toString());
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(tDailyRecordActivity.this,
android.R.layout.simple_list_item_1, list);
lv_dailyRecords.setAdapter(arrayAdapter);
}
});
}
}).start();
}
}
}