2011-02-09 4 views
0

메모장 튜토리얼을 기반으로 작업하고있는 앱입니다 ... 튜토리얼에서 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); 
    } 
}} 

답변

0

다음은 내가 생각하고있는 것입니다. Contact 클래스는 그것을 시작하는 인 텐트가 KEY_ROWID 키 아래에 Long 여분을 가져야한다고 기대합니다. onClick 메서드에서 문자열 null 값을 설정하고 있습니다. Contact.onCreate가이를 검색하려고하면 Long mRowId 변수에 할당 할 수없는 명시 적 null 문자열 값을 얻게됩니다. 전달할 데이터가 실제로 없으므로 putExtra를 호출하지 마십시오.

+0

저는 그가 연락처보기에서보고있는 rowid로 putExtra를 호출해야한다고 생각합니다. 편집 의도에서 편집하십시오. –

+0

아마도 OP가 원하는 일일 것입니다. 그러나 putExtra를 호출하지 않는 것이 좋습니다. 널 (또는 누락 된) rowid는 새 메모를 작성합니다. ('Edit.saveState()'를 참조하십시오.) **하지 않는 것은 KEY_ROWID 아래에'String' 객체 (널 또는 기타)를 저장하는 것입니다. –

+0

Ted Hopp - 맞아요. 편집 클래스에 데이터를 전달하고 싶지 않습니다. 편집 클래스로 이동하여 연락처 클래스에 표시된 사람의 레코드를 표시해야합니다. –

0

첫 번째 코드에서 onListItemClick()의 ​​id은 데이터베이스에서 KEY_ROWID로 사용되는 숫자 행 ID입니다. 그것이 여분의 데이터로 전달되는 이유입니다. 단추와 OnClick()을 사용하여 전환 할 때 단추 코드는 편집중인 ROWID가 아닌 연락처의보기 만 갖습니다. 그래서 당신은 버튼이 OnClick() 버튼을 클릭하면 그 rowid를 id 값으로 편집 클래스에 전달할 수 있도록 행 인 텐트를 Contact 인 텐트에 전달했는지 확인하려고합니다. 당신이 그랬던 것처럼 null로 설정할 수는 없습니다. 당신이보고있는 데이터베이스의 행 ID 일 필요가 있습니다. 버튼이 Edit 클래스를 호출 할 수 있습니다.

+0

codeboy2k - "어떻게 든 행의 ID를 연락처 의도에 전달"하는 것이 내 견과류를 운전하는 부분입니까? –

+0

기본 클래스 onListItemClick()이 이미 Contact 클래스에 rowid를 보내고있는 것 같습니다. Contact 활동을 시작한 인 텐트에서 해당 ROWID를 추출한 다음 Edit를 시작하는 인 텐트를 통해 Edit 활동으로 전달하면됩니다. Contact 클래스의 onClick() 버튼에서 this.getIntent()를 사용하여 Contact 활동을 시작한 인 텐트를 가져옵니다. 그런 다음 getExtras()를 사용하여 원래의 rowid를 검색하는 맵 (번들 유형)을 가져옵니다. 편집 활동을 시작하는 데 사용 된 새 인 텐트에 해당 ROWID를 넣으십시오. –

+0

codeboy2k =이 번들과 같은 것을 사용 하시겠습니까? extras = getIntent(). getExtras(); –

관련 문제