gpt4 book ai didi

android - 想从数据库中搜索名称而不是数据库中的最后一个字段

转载 作者:行者123 更新时间:2023-11-30 02:20:00 27 4
gpt4 key购买 nike

DBhelper 类

 public class DBHelper extends SQLiteOpenHelper{

private static final String DB_NAME_TWO = "Loan_TWO.db";
private static final int VERSION = 1;
private SQLiteDatabase mDb;

public DBHelper(Context context) {
super(context, DB_NAME_TWO, null, VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {


// db.execSQL("create table " + TABLE_COLLEGES + " ( "
// + COL_ID + " integer primary key autoincrement, "
// + COL_NAME + " text, "
// + COL_ADDRESS + " text);");


db.execSQL("create table " + LOAN_TABLE + " ( "
+ ID + " integer primary key autoincrement, "
+ Name + " text,"
+ START_DATE + " text, "
+ END_DATE + " text, "
+ LOAN_AMOUNT + " integer, "
+ RATE_OF_INTEREST + " integer ,"
+ FINAL_AMOUNT + " integer) ;");



db.execSQL("create table " + AMOUNT_TABLE_ONE + " ( "
+ AMOUNT_ID + " integer primary key autoincrement, "
+ ID + " integer, "
+ Name + " text, "
+ AMOUNT + " text, "
+ BALANCE_AMOUNT + " text, "
+ NEXT_DATE + " text, "
+ TODAYS_DATE + " text); ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + LOAN_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + AMOUNT_TABLE_ONE);

onCreate(db);
}
}

SearchActivity 类

    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;

private DBHelper helper;
Personal_loan p = new Personal_loan();

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() {
helper = new DBHelper(this);
db = helper.getWritableDatabase();

String columns[] = new String[] {ID,Name,START_DATE,END_DATE,LOAN_AMOUNT,RATE_OF_INTEREST,FINAL_AMOUNT};
Cursor cursor = db.query( true, LOAN_TABLE, columns,"Name=?",new String[]{"Amit"}, 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)
{

SearchActivity.this.adapter.getFilter().filter(s);
}
});

}

cursor.close();
db.close();
adapter.notifyDataSetChanged();
}

}


public void onBackPressed() {
// TODO Auto-generated method stub
Intent i=new Intent(SearchActivity.this,FirstActivity.class);
startActivity(i);
}
}

个人贷款

public class Personal_loan {
public int id;
String name;
String sd;
String ed;
Double la;
float roi;
String amt;


@Override
public String toString() {
return " Person ID =" + id + "\n Name =" + name + "\n Start Date =" + sd
+ "\n End Date =" + ed + "\n Loan Amount =" + la + "\n Rate Of Interest =" + roi +"\n Loan Final Amount= "+ amt ;
}
}

我想从数据库中搜索名称并根据在 edittext 上键入的名称字段过滤列表,上面的代码按最后一个字段(即最终金额)搜索列表,但我想通过以下方式搜索它名称不是最终金额。

最佳答案

您真的应该尝试使用 SimpleCursorAdapter 而不是 ArrayAdapter 并将 adapter.swapCursor(cursor) 与具有正确的新游标一起使用在 afterTextChanged 上查询。

可选,推荐使用loaders对于任何数据库访问,这样您就不会减慢主 ui 线程。

关于android - 想从数据库中搜索名称而不是数据库中的最后一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28715572/

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