2011-08-23 69 views
1

我在我的SQLite數據庫中有一個描述列,其中包含一些要粘貼到每個標記時顯示的文本。每個標記都有自己的描述。問題是當我點擊一個標記時,我會顯示所有其他AlertDialog。我使用下面的代碼:在地圖上點擊標記結果在AlertDialog中顯示所有其他地圖標記

@Override 
protected boolean onTap(int index) { 
    db = openHelper.getWritableDatabase(); 

    String[] result_columns = new String[] {COL_DESCRI}; 
    Cursor cur = db.query(true, TABLE_COORD, result_columns, null, null, null, null, null, null); 
    cur.moveToFirst(); 
    while (cur.isAfterLast() == false) { 
      String description = cur.getString(cur.getColumnIndexOrThrow("description")); 
      AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this); 
      dialog.setTitle("Infos."); 
      dialog.setMessage(description); 
      dialog.setPositiveButton("OK", new OnClickListener() {  
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }); 
      dialog.show(); 

      cur.moveToNext(); 
    } 
    cur.close(); 
    db.close(); 

    return true; 
} 

onTap功能似乎工作:

protected boolean onTap(int index) { 
    db = openHelper.getWritableDatabase(); 

    String[] result_columns = new String[] {COL_DESCRI}; 
    Cursor cur = db.query(true, TABLE_COORD, result_columns, null, null, null, null, null, null); 

    cur.moveToPosition(index); 
    String description = cur.getString(cur.getColumnIndexOrThrow("description")); 

    AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this); 
    dialog.setTitle("Infos."); 
    dialog.setMessage(description); 
    dialog.setPositiveButton("OK", new OnClickListener() {  
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
    }); 
    dialog.show(); 
    cur.close(); 

    db.close(); 

      return true; 
     } 

我知道我有一個指標分配到每一個AlertDialog,但我不知道怎麼做。我試圖解決這個問題,但我似乎無法做到。有關如何解決問題的任何建議?

回答

2

您似乎在循環播放結果中的所有項目。

cur.moveToFirst()移動您的第一個項目,然後cur.moveToNext();將您移到每次迭代的下一個項目。您然後顯示每個項目的警報對話框!

你真的不應該需要在用戶每次點擊一個項目的時間來加載這樣的數據,但最簡單的[不推薦]的方式來解決你的代碼是使用cur.moveToPosition(指數)代替你的循環。

+0

正是。你的意思是,我必須用'cur.moveToPosition(index)'替換'cur.moveToFirst()'並刪除'cur.moveToNext()'?爲什麼不推薦? – androniennn

+0

不建議使用,因爲您應該只加載一次數據 - 將其綁定到視圖時。您尚未發佈此代碼,因此很難發表評論。您應該將其加載到本地集合中。然後,當用戶點擊一個項目時,只需按索引訪問正確的項目。 – ShibbyUK

+0

我試過'cur.moveToPosition(index)',應用程序凍結,然後強制關閉。 – androniennn