2011-05-03 35 views
1

我有一個ListView將數據從數據庫中填充到複雜ListView中。數據庫'gotoURL'列中的字符串是一個URL,用於使用putExtra intent將鏈接加載到WebView中。當我點擊ListItem去下一個Activity時,我的方法抓取正確的字符串數據,但它從錯誤的db行中拉取字符串。根據我的'log.i(...);'在DDMS中指出正確的id行被選中,但是來自列的字符串是從第一行id中取出的,而不是從所選的行id中取出(???) - 它在所選的任何ListItem上執行此操作。ListView clickEvent選擇錯誤的數據庫行ID - 幫助!

我無法弄清楚如何編寫這個函數來正常工作。 Plz幫助示例代碼。謝謝。

我的活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.list_view2); 

    activityTitle = (TextView) findViewById(R.id.titleBarTitle); 
    activityTitle.setText("ADVISORY CIRCULATORS"); 

    displayResultList(); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setClickable(true); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> a, View v, int pos, 
       long id) { 

      String url = ""; 
      lv.getItemIdAtPosition(pos); 
      TextView tv = (TextView) lv.findViewById(R.id.dummy); 
      url = (String) tv.getTag(); 

      LLog.i("tag", "ID:" + id + "URL: " + url + " selected"); 
      Intent i = new Intent(List_AC.this, DocView.class); 
      i.putExtra("url", url); 
      startActivity(i); 
     } 
    }); 
} 

private void displayResultList() { 

    if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED)) { 
     extStorageDirectory = Environment.getExternalStorageDirectory() 
       .toString(); 

     File dbfile = new File(extStorageDirectory 
       + "/Aero-Technologies/flyDroid/dB/flyDroid.db"); 

     SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, 
       null); 

     Cursor databaseCursor = db.rawQuery(
       "SELECT * FROM AC_list ORDER BY `label` ASC", null); 

     Adapter_AC databaseListAdapter = new Adapter_AC(this, 
       R.layout.list_item, databaseCursor, new String[] { "label", 
         "title", "description", "gotoURL" }, new int[] { 
         R.id.label, R.id.listTitle, R.id.caption, 
         R.id.dummy }); 

     databaseListAdapter.notifyDataSetChanged(); 
     this.setListAdapter(databaseListAdapter); 

     } else if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_UNMOUNTED)) { 
      Log.i("tag", "SDCard is NOT writable/mounted"); 
      Alerts.sdCardMissing(this); 
     } 
    } 
} 

我的適配器:

public class Adapter_AC extends SimpleCursorAdapter { 


static Cursor dataCursor; 
private LayoutInflater mInflater; 

public Adapter_AC(Context context, int layout, Cursor dataCursor, 
     String[] from, int[] to) { 
    super(context, layout, dataCursor, from, to); 
    this.dataCursor = dataCursor; 
    mInflater = LayoutInflater.from(context); 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item, null); 

     holder = new ViewHolder(); 
     holder.text1 = (TextView) convertView.findViewById(R.id.label); 
     holder.text2 = (TextView) convertView.findViewById(R.id.listTitle); 
     holder.text3 = (TextView) convertView.findViewById(R.id.caption); 
     holder.text4 = (TextView) convertView.findViewById(R.id.dummy); 

     holder.text4.setVisibility(View.GONE); 

     convertView.setTag(holder); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    dataCursor.moveToPosition(position); 

    int label_index = dataCursor.getColumnIndex("label"); 
    String label = dataCursor.getString(label_index); 

    int title_index = dataCursor.getColumnIndex("title"); 
    String title = dataCursor.getString(title_index); 

    int description_index = dataCursor.getColumnIndex("description"); 
    String description = dataCursor.getString(description_index); 

    int goto_index = dataCursor.getColumnIndex("gotoURL"); 
    String gotoURL = dataCursor.getString(goto_index); 

    holder.text1.setText(label); 
    holder.text2.setText(title); 
    holder.text3.setText(description); 
    //holder.text4.setText(gotoURL); 
    holder.text4.setTag(gotoURL); 

    return convertView; 
} 

static class ViewHolder { 
    TextView text1; 
    TextView text2; 
    TextView text3; 
    TextView text4; 
} 

} 
+0

你爲什麼用三種不同的方式提出同樣的問題?我已經回答了你爲什麼在你的其他問題中得到錯誤的行(http://stackoverflow.com/questions/5838765/listview-putextra-from-db-column/5874043#5874043)。 – 2011-05-03 22:17:14

+0

@ColdForged我試過你的實現,但沒有得到任何回報(日誌貓顯示爲空)。是的,問題是相似的,但代碼已經改變。至少現在我有了什麼,一個字符串正在返回。此外,我重新提交,因爲當評論或編輯完成時,線程不會被推到'未答覆'Que的頂部(操作系統部分的設計不好 - 它位於相同的點/順序中)。 – CelticParser 2011-05-03 22:43:57

+0

所以這些帖子在幾個小時後停滯不前,在櫃檯上沒有更多的點擊。我感謝你的幫助,我仍然在繼續尋找你的方法的一個工作(這似乎更合乎邏輯)。因爲我被迫走上了一個陡峭的學習曲線(Java和Android的新手)來開發這個項目,除了操作系統以外沒有「真正的」資源,所以當我做出更接近的更改時,我必須重新提交我的q。 thnx再次,如果你願意讓我的答案更遠,我將不勝感激的幫助。 – CelticParser 2011-05-03 22:44:07

回答

1

確定您的onItemClick有幾件事情錯了(見在線評論):

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int pos, 
       long id) { 

      String url = ""; 
      lv.getItemIdAtPosition(pos); //you're never storing this value, but that's not the problem 


      //This "lv" is the main problem: you're asking the LISTVIEW for the TextView 
      //That's why you always get the first one. You should be asking the current view (v) instead 
      TextView tv = (TextView) lv.findViewById(R.id.dummy); 
      url = (String) tv.getTag(); 

      LLog.i("tag", "ID:" + id + "URL: " + url + " selected"); 
      Intent i = new Intent(List_AC.this, DocView.class); 
      i.putExtra("url", url); 
      startActivity(i); 
     } 
    }); 

但是,你爲什麼試圖使用持有者中的數據而不是獲得支持?

+0

Thnx !! @dmon就是這樣。由於我對Java和Android的知識淵博,這是返回任何類型數據(至少是某種東西)的唯一方式。如果您有更好的方法,請將其作爲另一個答案,並仔細查看我的其他活動和適配器代碼。我認爲@ColdForged有正確的想法[這裏](http://stackoverflow.com/questions/5862195/how-to-pass-a-string-into-onitemclick-listview/5862821#5862821),但它會返回我的數據爲空。 Thnx再次。 – CelticParser 2011-05-03 22:58:38