2012-07-04 51 views
0

訪問數據庫值我的網頁這樣的數據庫:上一個ListView

import android.provider.BaseColumns; 
    public interface ThreadDatabase extends BaseColumns { 
    String TABLE_NAME = "Threads"; 
    String TITLE = "Title"; 
    String DESCRIPTION = "Description"; 
    String URL = "Url"; 
    String[] COLUMNS = new String[] { _ID, TITLE, DESCRIPTION, URL }; 
} 

而且在我的活動與顯示標題和描寫的特徵每一行的SimpleCursorAdapter列表視圖。

Cursor c = dbhelper.getDatabase(); 
setListAdapter(new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_2, c, new String[] { 
        ThreadDatabase.TITLE, ThreadDatabase.DESCRIPTION }, 
      new int[] { android.R.id.text1, android.R.id.text2, }) 

我該如何重新定義onListItemClick方法,以便當我點擊該項目時正確的URL被打開?

protected void onListItemClick(ListView l, View v, int position, long id) { 
    ???Missing part??? 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
} 

回答

0

我假設你的活動擴展ListActivity。如果是這樣,在onListItemClick()中,您所需要做的就是:

protected void onListItemClick(ListView l, View v, int position, long id) { 

    Cursor item = (Cursor)getListAdapter().getItem(position); 

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
} 
0

您需要對SimpleCursorAdapter的引用,以便將其初始化爲字段。

假設你有一個字段mAdapter,你可以得到光標這樣的:

protected void onListItemClick(ListView l, View v, int position, long id) { 

    Cursor cursor = (Cursor) mAdapter.getItem(position);  

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent); 
} 

希望有所幫助。