我正在工作,並且基於記事本教程開發了一個應用程序...在教程中,您點擊listview中的一個記錄,然後您將被編輯顯示相應的信息,然後使你的改變選擇確認..並被帶回到主。需要關於onClick和意圖的幫助
我現在添加了第二個活動到應用程序「聯繫人」,當你點擊列表視圖中的一個筆記,你被聯繫到相應的信息顯示包括一個按鈕.......... ..你點擊按鈕,並採取編輯.........這是我的問題彈出其醜陋的頭。當我點擊按鈕一個空白的屏幕和一個強制關閉彈出........
這裏是從主要帶你聯繫的代碼....這個作品的信息顯示
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);」需要重新編碼用的onClick工作相比onListitemClick ........我想的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.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);
}
}}
我認爲他仍然需要調用putExtra與他在聯繫人視圖中查看的rowid,所以他可以在Edit intent中編輯它。 – 2011-02-09 20:10:50
這可能是OP想要發生的事情。但是,不要將它稱爲putExtra。 null(或缺失)rowid創建一個新註釋。 (請參閱`Edit.saveState()`。)**不做的事情是在KEY_ROWID下存儲一個String對象(null或否則)。 – 2011-02-09 20:46:27