2013-11-22 96 views
1

问题我正在使用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不能被解析为一个变量

任何想法如何解决这个问题?

回答

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的书。

+0

感谢您的答案,它有点作品,但现在我在Curso c = mNotesCurser得到一个错误; :mNotesCursor无法解析为变量。 有什么想法? – mind

+0

该死的...读书的人。你不明白最简单的事情。在这种情况下,你会问你有什么问题。阅读书籍的人...阅读书籍第一 - 关于语法。 –

+0

来吧,给我一个提示 – mind

1

替换为您的代码:

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

的Java doesn't allow只用数字来声明变量。

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