问题我正在使用developer.android记事本教程(http://developer.android.com/training/notepad/notepad-ex2.html) 来创建一个Noteapp, 但我有问题onItemClick和我弄不清,问题出在哪里。onListItemClick(mNotesCursor)
我的代码
public class MainActivity extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
public static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private int mNoteNumber = 1;
private NotesDbAdapter mDbHelper;
public static String notesstring;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
ImageButton b = (ImageButton) findViewById(R.id.add);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Intent i = new Intent(MainActivity.this, NoteScreen.class);
//startActivity(i);
createNote();
}
});
}
private void fillData() {
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainactivitymenu, menu);
return result;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
Intent i = new Intent(MainActivity.this, Settings.class);
startActivity(i);
return true;
/*case R.id.action_new:
Intent e = new Intent(MainActivity.this, NoteScreen.class);
startActivity(e);
return true;
*/
default:
return super.onOptionsItemSelected(item);
}
}
@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);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
private void createNote() {
Intent i = new Intent (this, NoteScreen.class);
startActivityForResult(i, ACTIVITY_CREATE);
/*String noteName = "Note " + mNoteNumber++;
mDbHelper.createNote(noteName, "");
fillData();*/
}
这是部分地方的错误是:
@Override
protected void onListItemClick(ListView list, View v, int position, long id) {
super.onListItemClick(list, v, position, id);
Cursor c = mNotesCurser;
c.moveToPosition(position);
Intent i = new Intent(this, NoteScreen.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Bundle extras = intent.getExtras();
switch(requestCode) {
case ACTIVITY_CREATE:
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.createNote(title, body);
fillData();
break;
case ACTIVITY_EDIT:
Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (rowId != null) {
String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
mDbHelper.updateNote(rowId, editTitle, editBody);
}
fillData();
break;
}
}
}
mNotesCurser不能被解析为一个变量
任何想法如何解决这个问题?
感谢您的答案,它有点作品,但现在我在Curso c = mNotesCurser得到一个错误; :mNotesCursor无法解析为变量。 有什么想法? – mind
该死的...读书的人。你不明白最简单的事情。在这种情况下,你会问你有什么问题。阅读书籍的人...阅读书籍第一 - 关于语法。 –
来吧,给我一个提示 – mind