gpt4 book ai didi

android - 我想使用 edittext 从数据库中搜索名称

转载 作者:行者123 更新时间:2023-12-05 07:56:30 26 4
gpt4 key购买 nike

public class SearchActivity extends Activity {

private EditText Search_By_Name;
private ArrayList<Personal_loan> array;
private ArrayAdapter<Personal_loan> adapter;
private ListView listView;
SQLiteDatabase db;
Cursor cursor ;
private DBHelper dbHelper;
Personal_loan p = new Personal_loan();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Search_By_Name = (EditText)findViewById(R.id.SearchName);
array = new ArrayList<Personal_loan>();
listView = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<Personal_loan>(this, android.R.layout.simple_list_item_1, array);
listView.setAdapter(adapter);
getData();
}
private void getData() {
DBHelper helper = new DBHelper(this);
db = helper.getReadableDatabase();
String columns[] = new String[] {ID,Name,START_DATE,END_DATE,LOAN_AMOUNT,RATE_OF_INTEREST,FINAL_AMOUNT};
cursor = db.query( false, LOAN_TABLE, columns, null, null, null, null, null, null, null);
if (!cursor.isAfterLast()) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
p = new Personal_loan();
p.id = cursor.getInt(0);
p.name = cursor.getString(1);
p.sd = cursor.getString(2);
p.ed = cursor.getString(3);
p.la = cursor.getDouble(4);
p.roi = cursor.getFloat(5);
p.amt=cursor.getString(6);
array.add(p);
cursor.moveToNext();
Search_By_Name.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
s=Search_By_Name.getText();
p.name=s.toString();
adapter.getFilter().filter(s.toString());
}
});
}
cursor.close();
db.close();
adapter.notifyDataSetChanged();
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i=new Intent(SearchActivity.this,FirstActivity.class);
startActivity(i);
}
}

这是我更新的代码,它过滤了数据但使用最后一个字段,而不是按名称字段...它使用最后一个字段进行过滤,如果有人知道答案请分享

最佳答案

试试这个……

  Search_By_Name .addTextChangedListener(new TextWatcher() {

@Override public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = search.getText().toString().toLowerCase(Locale.getDefault()); //
Your_Activity.this.adapter.filter(text); }

@Override
public void beforeTextChanged(CharSequence arg0, int
arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
} });

引用 link

关于android - 我想使用 edittext 从数据库中搜索名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28628859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com