今天开发安卓端的科技政策一点通,相比于web端有点复杂,边查资料边敲代码一直弄到十一点。
部分代码
package com.example.policy; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.example.policy.utils.MySQLDatabaseHelper; import java.util.List; public class MainActivity1 extends AppCompatActivity { private RecyclerView recyclerView; private PolicyAdapter policyAdapter; private List<Policy> policyList; private EditText searchEditText; private MySQLDatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); databaseHelper = new MySQLDatabaseHelper(); recyclerView = findViewById(R.id.recyclerView); searchEditText = findViewById(R.id.searchEditText); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 从数据库获取政策数据 policyList = databaseHelper.getAllPolicies(); //UserDao userDao = new UserDao(); //policyList= userDao.query2(); policyAdapter = new PolicyAdapter(policyList); recyclerView.setAdapter(policyAdapter); searchEditText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 启动 SearchActivity Intent intent = new Intent(MainActivity1.this, SearchActivity.class); String query = searchEditText.getText().toString(); intent.putExtra("query", query); startActivity(intent); } }); searchEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { filterPolicies(charSequence.toString()); } @Override public void afterTextChanged(Editable editable) { } }); } private void filterPolicies(String query) { List<Policy> filteredPolicyList = databaseHelper.searchPolicies(query); policyAdapter.updateList(filteredPolicyList); } }标签:int,void,searchEditText,private,new,import,5.28 From: https://www.cnblogs.com/zljzy/p/18241506