需要帮助onClick和意图

时间:2011-02-09 19:49:30

标签: android

我正在使用基于记事本教程的应用程序...在教程中,您单击列表视图中的注释,然后您将进行编辑,显示相应的信息,然后进行更改选择确认。 。并被带回Main。

我现在在应用程序“联系人”中添加了第二个活动,当你点击列表视图中的一个笔记并且你被联系时,相应的信息显示包括一个按钮...........你单击按钮并进行编辑.........这就是我的问题弹出它丑陋的脑袋。当我按下按钮时,得到一个空白屏幕并强制关闭弹出窗口........

以下是主要的代码,您可以联系....这样可以显示信息

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, Contact.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

这是从联系到编辑的代码.....这给了我错误,我确定它与“intent.putExtra(NotesDbAdapter.KEY_ROWID,id);”有关。与onListitemClick相比需要重新编码与onClick一起工作........我认为id没有被传递

public void onClick(View v) {
String id = null;
switch (v.getId()) {
case R.id.admin: // doStuff
    Intent intent = new Intent(this, Contact.class);
    intent.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivity(intent); 
    break;

有人可以通过按钮????

告诉我如何做到这一点

这是Main,Contacts和Edit的代码..........如果你需要它.......谢谢

Main.class

public class Main extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;

private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private static final int ABOUT = Menu.FIRST + 2;
private static final int TESTBUTTON = Menu.FIRST + 3;
private static final int CONTACT_ID = Menu.FIRST + 4;
private static final int CANCEL_ID = Menu.FIRST + 5;

private NotesDbAdapter mDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_NAME, NotesDbAdapter.KEY_RANK};


    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1, R.id.text2};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.mainlistrow, notesCursor, from, to);
    setListAdapter(notes);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    menu.add(0, ABOUT, 0, R.string.menu_about);
    menu.add(0, TESTBUTTON, 0, R.string.menu_test);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case INSERT_ID:
            createNote();
            return true;
        case ABOUT:        
            menuabout();        
            return true;
        case TESTBUTTON:        
            menutest();        
            return true;    
    }

    return super.onMenuItemSelected(featureId, item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, CONTACT_ID, 0, R.string.menu_contact);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    menu.add(0, CANCEL_ID, 0, R.string.menu_cancel);

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteNote(info.id);
            fillData();
            return true;
        case CONTACT_ID:
            contact();
            return true;   
    }
    return super.onContextItemSelected(item);
}

private void menutest() {
    Intent i = new Intent(this, Newmain.class);
    startActivityForResult(i, ACTIVITY_CREATE);
    Log.i(this.toString(), "menu test");
}

private void contact() {
    Intent i = new Intent(this, Contact.class);
    startActivityForResult(i, ACTIVITY_CREATE);
    Log.i(this.toString(), "menu contact");
}    

private void createNote() {
    Intent i = new Intent(this, Admin.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

public void menuabout() {
    final Dialog dialog = new Dialog(Main.this);
    dialog.setContentView(R.layout.about);
    dialog.setTitle("About NCO Leaders Book");      
    dialog.setCancelable(true);
    Button button = (Button) dialog.findViewById(R.id.AboutButton);
    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
              });
              //now that the dialog is set up, it's time to show it    
              dialog.show();
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, Contact.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}

}

Contact.class

    public class Contact extends Activity implements OnClickListener{    
    private EditText mNameText;
    private EditText mRankText;
    private Long mRowId;
    private NotesDbAdapter mDbHelper;
    protected Cursor cursor;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.contact);

        Button adminButton = (Button) this.findViewById(R.id.admin);
        adminButton.setOnClickListener(this); 

        mNameText = (EditText) findViewById(R.id.name);
        mRankText = (EditText) findViewById(R.id.rank);

        mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                    : null;}

          populateFields();
    };


private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchsoldiers(mRowId);
        startManagingCursor(note);
        mNameText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_NAME)));
        mRankText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_RANK)));
    }
}

//@Override
public void onClick(View v) {
    String id = null;
    switch (v.getId()) {
    case R.id.admin: // doStuff
        Intent intent = new Intent(this, Edit.class);
        intent.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivity(intent);
        Log.i("Onclick", "EndingTestNext");    
        break;
}}

}

Edit.class

public class Edit extends Activity {

private EditText mNameText;
private EditText mRankText;
private EditText mSsnText;
private Long mRowId;
private NotesDbAdapter mDbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.edit);
    setTitle(R.string.edit_note);

    mNameText = (EditText) findViewById(R.id.name);
    mRankText = (EditText) findViewById(R.id.rank);
    mSsnText = (EditText) findViewById(R.id.ssn);

    Button confirmButton = (Button) findViewById(R.id.confirm);


    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }

    populateFields();

    confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }

    });
}

private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mNameText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_NAME)));
        mRankText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_RANK)));
        mSsnText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_SSN)));
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}

@Override
protected void onPause() {
    super.onPause();
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    populateFields();
}

private void saveState() {
    String name = mNameText.getText().toString();
    String rank = mRankText.getText().toString();
    String ssn = mSsnText.getText().toString();

    if (mRowId == null) {
        long id = mDbHelper.createNote(name, rank, ssn);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateNote(mRowId, name, rank, ssn);
    }
}}

2 个答案:

答案 0 :(得分:0)

这就是我的想法。 Contact类期望启动它的Intent在KEY_ROWID键下应该有一个Long extra。在onClick方法中,您要设置 String null值。当Contact.onCreate尝试检索它时,它会获得一个显式的null String值,该值不能分配给Long mRowId变量。只是不要调用putExtra,因为你实际上没有任何数据可以通过。

答案 1 :(得分:0)

在第一个代码中,onListItemClick()中的id是一个数字行id,在数据库中用作KEY_ROWID。这就是它作为额外数据传递的原因。在切换到使用按钮和OnClick()时,按钮代码只有(联系人)视图,而不是您正在编辑的rowid。因此,您希望确保以某种方式将行ID传递给Contact intent,以便按钮单击OnClick()可以将该rowid作为id值传递到Edit类中。您不能像过程那样将其设置为null。它确实需要是您正在查看的数据库中的行ID,因此您的按钮可以调用Edit类