I want to know the most effective way to extract data from android SQLite Database Cursor into an array. The problem is when i'm using dialog of list (AlertDialog.Builder.setItems) the items are set from an array. To generate the array I need to go through each row of the Cursor and regenerate new array based on the values, and it takes extremely longer time to process.
Maybe I’m missing something like c++ std::vector or python's list where i can push/append and pop quickly. And I’m wondering how do people work on their dialog of list which fetch data from database and keep it fast to process. thanks
//-- Draw all list of base account
dbv_accounts = db.rawQuery("SELECT * FROM accounts WHERE type='BASE'",null);
CharSequence[] accounts_list = {};
if (dbv_accounts.getCount()>0)
while (dbv_accounts.moveToNext()) //-- cursor starts from -1
{
CharSequence[] new_accountl = new CharSequence[accounts_list.length + 1];
System.arraycopy(accounts_list, 0, new_accountl, 0, accounts_list.length);
new_accountl[accounts_list.length] = dbv_accounts.getString(dbv_accounts.getColumnIndex("name"));
accounts_list = new_accountl;
}
db.close();
final CharSequence[] finalAccounts_list = accounts_list;
bt_Cba_Baseaccount.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ChangebaseaccountActivity.this);
builder.setTitle("Pick Base Account Name");
builder.setItems(
finalAccounts_list,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item){
bt_Cba_Baseaccount.setText(finalAccounts_list[item]);
String ba = finalAccounts_list[item].toString();
}
}
);
builder.show();
}
}
);
Aucun commentaire:
Enregistrer un commentaire