메모장 튜토리얼을 기반으로 작업하고있는 앱입니다 ... 튜토리얼에서 listview의 메모를 클릭하면 해당 정보가 표시되었지만 편집하려면 변경 사항을 확인하고 메인으로 돌아갑니다.onClick 및 intents에 대한 도움이 필요합니다.
나는 목록보기에서 메모를 클릭하고 해당 정보가 버튼을 포함하여 표시되면 연락처 "app"에 두 번째 활동을 추가했습니다. .. 당신은 버튼을 클릭하고 편집하게됩니다 ......... 그리고 이것은 내 문제가 그 못생긴 머리를 터뜨리는 것입니다. 내가 버튼을 클릭하면 빈 화면과 강제 닫기 팝업이 나타납니다. ........
여기에서 연락 주신 코드가 있습니다 ....이 정보가 표시됩니다.
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에 비해 온 클릭 작업을 코딩 할 ........ 나는 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;
이상
는 사람이 어떻게 버튼에서이 작업을 수행하는 저를 보여줄 수 ???? 당신이 그것을 필요로하는 경우에 여기에
감사합니다 ....... ..........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();
}
Main.class가
홈페이지, 연락처 및 편집의 코드입니다 }
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
,745,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);
}
}}
저는 그가 연락처보기에서보고있는 rowid로 putExtra를 호출해야한다고 생각합니다. 편집 의도에서 편집하십시오. –
아마도 OP가 원하는 일일 것입니다. 그러나 putExtra를 호출하지 않는 것이 좋습니다. 널 (또는 누락 된) rowid는 새 메모를 작성합니다. ('Edit.saveState()'를 참조하십시오.) **하지 않는 것은 KEY_ROWID 아래에'String' 객체 (널 또는 기타)를 저장하는 것입니다. –
Ted Hopp - 맞아요. 편집 클래스에 데이터를 전달하고 싶지 않습니다. 편집 클래스로 이동하여 연락처 클래스에 표시된 사람의 레코드를 표시해야합니다. –