onListItemClick的问题(mNotesCursor)

时间:2013-11-22 22:12:27

标签: java android listview override variable-declaration

我正在使用developer.android记事本教程(http://developer.android.com/training/notepad/notepad-ex2.html) 创建Noteapp, 但我有onItemClick的问题,我无法弄清楚问题出在哪里。

我的代码

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无法解析为变量

知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

更改

protected void onListItemClick(ListView 1, View v, int position, long id)

protected void onListItemClick(ListView list, View v, int position, long id)

并使用list变量与ListView进行通信。

注意:永远不要将数字用作变量名的单个字符。 Java限制了这一点。

在开始为Android创建应用程序之前,我建议您至少阅读一本关于Java的书。

答案 1 :(得分:1)

用以下代码替换您的代码:

@Override
protected void onListItemClick(ListView listView, View v, int position, long id) {
     super.onListItemClick(listView, v, position, id);

Java doesn't allow仅使用数字声明变量。

  

变量的名称可以是任何合法标识符 - 无限长度   Unicode字母和数字的序列,以字母开头,   美元符号“$”,或下划线字符“_”