首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >onListItemClick问题(mNotesCursor)

onListItemClick问题(mNotesCursor)
EN

Stack Overflow用户
提问于 2013-11-23 06:12:27
回答 2查看 131关注 0票数 1

我正在使用Noteapp记事本教程(http://developer.android.com/training/notepad/notepad-ex2.html)来创建一个developer.android,但是我的Noteapp出现了问题,我不知道问题出在哪里。

我的代码

代码语言:javascript
运行
复制
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();*/
 }

这就是错误所在:

代码语言:javascript
运行
复制
@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解析为变量

你知道怎么解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2013-11-23 06:14:01

变化

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

票数 2
EN

Stack Overflow用户

发布于 2013-11-23 06:15:54

将您的代码替换为:

代码语言:javascript
运行
复制
@Override
protected void onListItemClick(ListView listView, View v, int position, long id) {
     super.onListItemClick(listView, v, position, id);

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20155469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档