我正在开发一个记事本应用程序。我的列表项目显示在NoteList中,当我点击它们时出现此错误。我不知道如何解决这个问题。有谁知道为什么会这样?
logcat的
04-18 18:20:53.430: E/AndroidRuntime(26517): FATAL EXCEPTION: main
04-18 18:20:53.430: E/AndroidRuntime(26517): java.lang.ClassCastException: com.example.note.contentprovider.NoteItem cannot be cast to android.database.Cursor
04-18 18:20:53.430: E/AndroidRuntime(26517): at com.example.note.contentprovider.NoteList.onListItemClick(NoteList.java:89)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.widget.AbsListView.performItemClick(AbsListView.java:1110)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2894)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.widget.AbsListView$1.run(AbsListView.java:3722)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.os.Handler.handleCallback(Handler.java:615)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.os.Looper.loop(Looper.java:137)
04-18 18:20:53.430: E/AndroidRuntime(26517): at android.app.ActivityThread.main(ActivityThread.java:4929)
04-18 18:20:53.430: E/AndroidRuntime(26517): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 18:20:53.430: E/AndroidRuntime(26517): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 18:20:53.430: E/AndroidRuntime(26517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
04-18 18:20:53.430: E/AndroidRuntime(26517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
04-18 18:20:53.430: E/AndroidRuntime(26517): at dalvik.system.NativeStart.main(Native Method)
NoteList.java
public class NoteList extends ListActivity implements LoaderManager.LoaderCallbacks < Cursor > {
private SimpleCursorAdapter dataAdapter;
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private static final int DELETE_ID = Menu.FIRST;
private NotesDbAdapter mDbHelper;
private ArrayList < NoteItem > notes;
private NoteClassAdapter aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notelist);
notes = new ArrayList < NoteItem > ();
int resID = R.layout.notes_row;
aa = new NoteClassAdapter(this, resID, notes);
setListAdapter(aa);
getLoaderManager().initLoader(0, null, this);
//registerForContextMenu(getListView());
Button addnote = (Button) findViewById(R.id.addnotebutton);
addnote.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
createNote();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
Bundle bundle = new Bundle();
bundle.putString("mode", "add");
i.putExtras(bundle);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor cursor = (Cursor) l.getItemAtPosition(position);
long row_id = cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID);
Intent i = new Intent(this, NoteEdit.class);
Bundle bundle = new Bundle();
bundle.putString("mode", "update");
bundle.putLong(NotesDbAdapter.KEY_ROWID, row_id);
i.putExtras(bundle);
// i.putExtra(NotesDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
// mDbHelper.deleteNote(info.id);
Uri uri = Uri.parse(NotesDbAdapter.CONTENT_URI + "/" + info.id);
getContentResolver().delete(uri, null, null);
return true;
}
return super.onContextItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
}
protected void onResume() {
super.onResume();
//Starts a new or restarts an existing Loader in this manager
getLoaderManager().restartLoader(0, null, this);
}
// This is called when a new Loader needs to be created.
@Override
public Loader < Cursor > onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader = new CursorLoader(this,
NotesDbAdapter.CONTENT_URI, null, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader < Cursor > loader, Cursor cursor) {
// dataAdapter.swapCursor(cursor);
int keyTaskIndex = cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE);
notes.clear();
while (cursor.moveToNext()) {
NoteItem newItem = new NoteItem(cursor.getString(keyTaskIndex));
notes.add(newItem);
}
aa.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader < Cursor > loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
// dataAdapter.swapCursor(null);
}
}
NoteItem.java
public class NoteItem {
String task;
Date created;
public String getTask() {
return task;
}
public Date getCreated() {
return created;
}
public NoteItem(String _task) {
this(_task, new Date(java.lang.System.currentTimeMillis()));
}
public NoteItem(String _task, Date _created) {
task = _task;
created = _created;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String dateString = sdf.format(created);
return "(" + dateString + ") " + task;
}
}
答案 0 :(得分:0)
Cursor cursor = (Cursor) l.getItemAtPosition(position);
不返回游标,它应返回ListItem
。更改您的引用以使用ListItem,并将该行更改为:
ListItem item = (ListItem) l.getItemAtPosition(position);