package com.example.yibaifen;
import static androidx.core.content.ContentProviderCompat.requireContext;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.yibaifen.Bean.Zhiyuan;
import com.example.yibaifen.Dao.Mysqldb;
import java.util.ArrayList;
import java.util.List;
public class Findmyzhiyuan extends AppCompatActivity {
// UI组件声明
private TextView textViewSample;
private RecyclerView mRecyclerView;
private MyAdapter mMyAdapter;
private EditText search;
private List<Zhiyuan> myzhiyuanList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_findmyzhiyuan);
Intent intent = getIntent();
String pub = intent.getStringExtra("pub");
// 初始化UI组件
textViewSample = findViewById(R.id.textViewSample1);
mRecyclerView = findViewById(R.id.rev);
search = findViewById(R.id.search_edit_text);
// 初始化RecyclerView
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
// 加载数据
loadZhiyuanData();
// 监听搜索框内容变化
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
filterZhiyuan(s.toString());
}
@Override
public void afterTextChanged(Editable s) {}
});
// 初始化Adapter
mMyAdapter = new MyAdapter(myzhiyuanList);
mRecyclerView.setAdapter(mMyAdapter);
}
private void loadZhiyuanData() {
new Thread(new Runnable() {
@Override
public void run() {
Mysqldb mysqldb = new Mysqldb();
ArrayList<Zhiyuan> zhiyuans = mysqldb.getzhiyuan();
if (zhiyuans != null) {
myzhiyuanList.addAll(zhiyuans);
// 切换到主线程刷新数据
runOnUiThread(new Runnable() {
@Override
public void run() {
mMyAdapter.notifyDataSetChanged();
}
});
}
}
}).start();
}
private void filterZhiyuan(String query) {
new Thread(new Runnable() {
@Override
public void run() {
Mysqldb mysqldb = new Mysqldb();
List<Zhiyuan> foundList = mysqldb.get_find_zhiyuan(query);
// 切换到主线程更新数据
runOnUiThread(new Runnable() {
@Override
public void run() {
myzhiyuanList.clear();
if (foundList != null && !foundList.isEmpty()) {
myzhiyuanList.addAll(foundList);
}
mMyAdapter.notifyDataSetChanged();
}
});
}
}).start();
}
// 自定义Adapter
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Zhiyuan> dataList;
public MyAdapter(List<Zhiyuan> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list2, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Zhiyuan zhiyuan = dataList.get(position);
holder.bind(zhiyuan);
}
@Override
public int getItemCount() {
return dataList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView1, textView2, textView3, textView4, textView5;
Button button1;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.textView1);
textView2 = itemView.findViewById(R.id.textView2);
textView3 = itemView.findViewById(R.id.textView3);
textView4 = itemView.findViewById(R.id.textView4);
textView5 = itemView.findViewById(R.id.textView5);
button1 = itemView.findViewById(R.id.button1);
}
void bind(Zhiyuan zhiyuan) {
textView1.setText("标题:" + zhiyuan.biaoti);
textView2.setText("标签:" + zhiyuan.biaoqian);
textView3.setText("地点:" + zhiyuan.didian);
textView4.setText("总需人数:" + zhiyuan.renshu);
textView5.setText("现有人数:" + zhiyuan.crenshu);
// 添加点击事件处理(此处假设button1是用来响应点击加入的操作)
button1.setOnClickListener(v -> {
// 这里处理点击加入的逻辑,例如调用Mysqldb的相关方法进行数据库操作
// ...
});
}
}
}
}