这个程序的主要动机是在Android ListView中显示来自SQLite的一些数据。
protected String doInBackground(String... args) {
SQLiteOpenHelper helper = new Dictionary(contex);
SQLiteDatabase db = helper.getReadableDatabase();
EditText et = (EditText) findViewById(R.id.search);
String word = et.getText().toString();
wordlist = new ArrayList<>();
String query = "SELECT ID, WORD" +
" FROM E_DICT WHERE WORD LIKE '"+word+"%' ORDER BY WORD ASC LIMIT 100 ";
Cursor cursor = db.rawQuery(query, null);
if(cursor!=null){
if(cursor.moveToFirst()){
do{
long a = cursor.getLong(0);
String b = cursor.getString(1);
HashMap<String, String> map = new HashMap<String, String>();
map.put(EDICT_WORD, b);
map.put(EDICT_ID, ""+a);
Log.d("RESULT", a +" " + b); //Till this this like everything seems to be ok.
wordlist.add(map);
}
while(cursor.moveToNext());
}}
return null;
}
现在我将数据加载到某些AsyncTask<String, String, String>
中,如下所示:
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
Main.this, wordlist, R.layout.word_list,
new String[] {EDICT_WORD, EDICT_ID},
new int[] {R.id.eDict_Word, R.id.eDict_Id});
setListAdapter(adapter);
}
});
}
这只给了我作为listview输出的最后一个EDICT_ID
。我在哪里做错了?
答案 0 :(得分:0)
您似乎总是在创建新的HashMap,并且只返回一个值。
所以,从
移动你的HashMapif(cursor!=null){
if(cursor.moveToFirst()){
....
HashMap<String, String> map = new HashMap<String, String>();
while(cursor.moveToNext());
}}
到循环之外。
HashMap<String, String> map = new HashMap<String, String>();
if(cursor!=null){
....
}