2

I'm working on an android app to list the bookmarks stored in the browser and populated onto a list using a cursor, and cursor adapter. I've had no problem populating the list at all, my problem is all in the click. I'm using an onItemClickListener and when the user clicks a list item the app crashes.

The logcat out puts this:

10-27 23:54:22.718: ERROR/CursorWindow(315): Bad request for field slot 0,2131034113.    
numRows = 15, numColumns = 12

10-27 23:54:22.738: ERROR/AndroidRuntime(315): java.lang.IllegalStateException: get    
field slot from row 0 col 2131034113 failed

below is my code.

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Browser;
import android.provider.Browser.BookmarkColumns;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;



@SuppressWarnings("unused")
public class BookmarksActivity extends ListActivity {
/** Called when the activity is first created. */
@Override   
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Uri bookmarks = Browser.BOOKMARKS_URI;
     final Cursor managedCursor = managedQuery(bookmarks, null, null, null, null );
   // Cursor managedCursor = managedQuery(bookmarks, bookmark, null, null,
BookmarkColumns.TITLE  );

    String[] bookmark = new String[]{
        BookmarkColumns.TITLE,
        BookmarkColumns.URL,
        BookmarkColumns._ID,
        };


    // Bind to our new adapter.


    int[] views = new int[] {R.id.urlTitle, R.id.urlUrl};

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main,
managedCursor, bookmark, views);
    this.setListAdapter(adapter);

    //list = (listView)adapter;

   this.getListView().setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            // TODO Auto-generated method stub
            Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(managedCursor.getString(R.id.urlUrl)));
            startActivity(i);
        }

    });

    getColumnData(managedCursor);


}



//getColumnData will go through the supplied cursor and get the data we need
// in this case webpage titles and urls.
// i will late implement the favicon.
 private void getColumnData(Cursor cur)
 {
    if (cur.moveToFirst()){

        String title;
        String url;

        int titleColumn = cur.getColumnIndex(BookmarkColumns.TITLE);
        int urlColumn = cur.getColumnIndex(BookmarkColumns.URL);

        String imagePath;

        do{
            title = cur.getString(titleColumn);
            url = cur.getString(urlColumn);

            Log.d("+-CURSOR-+", title + " " + url);

        }while(cur.moveToNext());

    }




 }
}
  • The answer on the following link looks good. Note that you're not even using "position" in your onItemClick. http://stackoverflow.com/questions/5553176/android-simplecursoradapter-setonitemclicklistener-problem – aleph_null Oct 28 '11 at 01:50

1 Answers1

1

the method onItemClick has the parameters

AdapterView<?> parent, View view, int position, long id

i'm not sure if your managedCursor is already moved to the correct position upon using it inside your onItemClick. you can try calling its move method first

c.moveToPosition(position)
josephus
  • 8,284
  • 1
  • 37
  • 57
  • "...i'm not sure if your managedCursor is already moved to the correct..." thats why normally we using `Cursor c = (Cursor)parent.getItemAtPosition(position);` in `onItemClick(AdapterView> parent, View view, int position, long id)` – Selvin Oct 28 '11 at 02:14
  • Thanks for the Replies but I'm still getting the same error out put. The cursor seems to be moving to the correct row. If i click the first item in the list it shows row 0 the second row 1 and so on, my problem is the column. it is looking at column 2131034113 which is non-existent. – Jeff Savaglio Oct 29 '11 at 16:55