package com.example.ke;标签:information,void,模糊,查询,科技,new,import,android,public From: https://www.cnblogs.com/bdsz/p/18257469
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.ke.R;
import com.example.ke.dao.UserDao;
import com.example.ke.pojo.Information;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_searchWhat;
private Button btn_search;
private ListView lv_information;
private UserDao userDao;
private Handler mainHandler;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userDao = new UserDao();
et_searchWhat = findViewById(R.id.et_searchWhat);
btn_search = findViewById(R.id.btn_search);
lv_information = findViewById(R.id.lv_information);
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<Information> information = userDao.getMyInformation(searchWhat);
List<String> list = new ArrayList<>();
mainHandler.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < information.size(); i++) {
list.add(information.get(i).getName());
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, list);
lv_information.setAdapter(arrayAdapter);
lv_information.setOnItemClickListener(new AdapterView.OnItemClickListener() {
int selectedPosition;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Information selectedInformation = information.get(position);
selectedPosition = position;
// 查询政策内容并显示在弹窗中
showPolicyContentDialog(selectedInformation);
}
});
}
});
}
}).start();
}
}
// 查询政策内容并显示在弹窗中
private void showPolicyContentDialog(Information information) {
new Thread(new Runnable() {
@Override
public void run() {
// 根据政策名称查询政策内容
String policyContent = userDao.getDetailText(information.getName()).toString();
mainHandler.post(new Runnable() {
@Override
public void run() {
// 显示政策内容的弹窗
showPolicyContentDialog(information.getName(), policyContent);
}
});
}
}).start();
}
// 显示政策内容的弹窗
private void showPolicyContentDialog(String policyName, String policyContent) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(policyName); // 设置弹窗标题为政策名称
builder.setMessage(policyContent); // 设置弹窗内容为政策内容
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // 点击确定按钮后关闭弹窗
}
});
builder.create().show(); // 创建并显示弹窗
}
}