I read bookmarks from database, show it by a listview, but when click
list item it can't load url correctly, some codes from browser app
source.

I have not much experience about android and java, this trouble have
confused me two days

Thanks for your help

public class bookmarks extends Activity {
private Cursor bookmarks;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bookmarks = Browser.getAllBookmarks(getContentResolver());
    int urlColumn =
bookmarks.getColumnIndex(android.provider.Browser.BookmarkColumns.URL);
    Cursor results;
    String[] proj = new String[]{
        android.provider.Browser.BookmarkColumns.URL,
        android.provider.Browser.BookmarkColumns.TITLE
    };
    int[] to = new int[]{
        android.R.id.text2,
        android.R.id.text1
    };

    String whereClause;
    String orderBy = Browser.BookmarkColumns.VISITS + " DESC";
    whereClause = Browser.BookmarkColumns.BOOKMARK + " != 0";

    results = managedQuery(Browser.BOOKMARKS_URI,
            Browser.HISTORY_PROJECTION, whereClause, null, orderBy);
    startManagingCursor(results);

    ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, results, proj, to);
    ListView myListView = (ListView) findViewById(R.id.list);
    myListView.setAdapter(adapter);
    myListView.setOnItemClickListener(mListener);       // 设置点击

}

// 侦听事件
private AdapterView.OnItemClickListener mListener = new
AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View v, int
position, long id) {
        // TODO Auto-generated method stub
        loadUrl(position);
    }

};

// 载入网址
private void loadUrl(int position) {
    Intent intent = (new Intent()).setAction(getUrl(position));
    setResultToParent(RESULT_OK, intent);
    finish();
}

public String getUrl(int position) {
    String url = getString(Browser.HISTORY_PROJECTION_URL_INDEX,
position);
    return url;
}

// This Activity is generally a sub-Activity of
CombinedHistoryActivity. In
// that situation, we need to pass our result code up to our parent.
// However, if someone calls this Activity directly, then this has no
// parent, and it needs to set it on itself.
private void setResultToParent(int resultCode, Intent data) {
    Activity a = getParent() == null ? this : getParent();
    a.setResult(resultCode, data);
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to