我正在开发一个Android应用程序,列出存储在浏览器中的书签,并使用游标和游标适配器填充到列表中。我根本没有填充列表的问题,我的问题就在于点击。我正在使用onItemClickListener,当用户单击列表项时,应用程序崩溃。
logcat out将此:
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
下面是我的代码。
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());
}
}
}
答案 0 :(得分:1)
onItemClick方法有参数
AdapterView<?> parent, View view, int position, long id
我不确定您的managedCursor在onItemClick中使用后是否已经移动到正确的位置。您可以先尝试调用move
方法
c.moveToPosition(position)