2015-10-18 99 views
1

这个程序的主要动机是在Android ListView中显示一些来自SQLite的数据。HashMap只给我最后一个数据

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); 
       } 
      }); 
     } 

这给了我最后EDICT_ID只列表视图输出。我在哪里做错了?

+0

您的数据库中有多少数据?而且,wordlist有多少项? –

回答

0

看来你总是在创建新的HashMap,并且只返回一个值。

所以,从

if(cursor!=null){ 
    if(cursor.moveToFirst()){ 
    .... 
    HashMap<String, String> map = new HashMap<String, String>(); 
    while(cursor.moveToNext()); 
}} 

移动你的HashMap来循环之外。

HashMap<String, String> map = new HashMap<String, String>(); 
if(cursor!=null){ 
    .... 
} 
+0

但是,HashMap正被添加到'wordList',它是一张地图列表。 – Cinnam

+0

哦,没错。 –

相关问题