2011-10-04 46 views
0

在這裏,我試圖將我的RowId傳遞給另一個活動,但它打印null.in myprofile類,我試圖傳遞行ID。請任何人幫助我。如何在android中傳遞RowId?

我的資料類

public class MyProfile extends Activity implements OnClickListener 
{ 

private MyProfileDb mDbHelper; 

private TextView mAllergiesText; 
private TextView mConditionsText; 
private TextView mBloodText; 
private TextView mNoteText; 
private long mRowId ; 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.myprofile); 

    mDbHelper = new MyProfileDb(this); 
    mDbHelper.open(); 

    View home = findViewById(R.id.home); 
    home.setOnClickListener(this); 

    View add = findViewById(R.id.add); 
    add.setOnClickListener(this); 

    mAllergiesText = (TextView) findViewById(R.id.allergy); 
    mConditionsText = (TextView) findViewById(R.id.condition1); 
    mBloodText = (TextView) findViewById(R.id.blood1); 
    mNoteText = (TextView) findViewById(R.id.note); 



    Cursor c = mDbHelper.fetchAlldetails(); 

    if (c.getCount() >= 1) 
    { 

     populateFields(c.getCount()); 
     c.close(); 
    } 

} 

public void onClick(View v) 
{  
    switch (v.getId()) 
    {  
    case R.id.home: 
     mDbHelper.close(); 
     Intent homeActivity = new Intent(this, Menu.class); 
     startActivity(homeActivity); 
     finish(); 
     break; 

    case R.id.add: 
     Intent addActivity = new Intent(this, EditMyProfile.class); 
     addActivity.putExtra("id", String.valueOf(mRowId)); 
      System.out.println("id is"+ mRowId); 

     startActivity(addActivity); 

     mDbHelper.close(); 
     break; 
    } 
} 

private void populateFields(int mRowId) 
{ 
    Cursor profile = mDbHelper.fetchdetails(mRowId); 
    startManagingCursor(profile); 

    mAllergiesText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_ALLERGIES))); 
    mConditionsText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_CONDITIONS))); 
    mBloodText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_BLOOD))); 
    mNoteText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_NOTE))); 

    profile.close(); 
} 

@Override 
public final boolean onKeyDown(int keyCode, KeyEvent event) { 

    if (KeyEvent.KEYCODE_BACK == keyCode) { 
     startActivity(new Intent(this, Menu.class)); 
     onDestroy(); 
    } 
    return super.onKeyDown(keyCode, event); 

} 

} 

editmyprofile類

public class EditMyProfile extends Activity implements OnClickListener { 


private EditText mAllergiesText; 
private EditText mConditionsText; 
private EditText mBloodText; 
private EditText mNoteText; 

private Long mRowId; 
private MyProfileDb mDbHelper; 
private int i; 
//private Long id; 



@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.editmyprofile); 

    mDbHelper = new MyProfileDb(this); 
    mDbHelper.open(); 

    View profilecancel = findViewById(R.id.profilecancel); 
    profilecancel.setOnClickListener(this); 

    mAllergiesText = (EditText) findViewById(R.id.allergies); 
    mConditionsText = (EditText) findViewById(R.id.condition); 
    mBloodText = (EditText) findViewById(R.id.blood); 
    mNoteText = (EditText) findViewById(R.id.note); 

    View save = findViewById(R.id.profilesave); 
    save.setOnClickListener(this); 

    View doccancel = findViewById(R.id.profilecancel); 
    doccancel.setOnClickListener(this); 

    View homeText = findViewById(R.id.homeInEditProfile); 
    homeText.setOnClickListener(this); 

    View home = findViewById(R.id.home); 
    home.setOnClickListener(this); 

    mRowId = Long.parseLong(getIntent().getStringExtra("id")); 

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

} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.profilecancel: 
     mDbHelper.close(); 
     startActivity(new Intent(this, MyProfile.class)); 
     finish(); 
     break; 

    case R.id.profilesave: 
     saveState(); 
     mDbHelper.close(); 
     startActivity(new Intent(this, MyProfile.class)); 
     break; 

    case R.id.homeInEditProfile: 
    case R.id.home: 
     mDbHelper.close(); 
     startActivity(new Intent(this, Menu.class)); 
     break; 
    } 
} 

private void saveState() 
{ 
    String allergies = mAllergiesText.getText().toString(); 
    String conditions = mConditionsText.getText().toString(); 
    String bloodgroup = mBloodText.getText().toString(); 
    String note = mNoteText.getText().toString();  


    if (mRowId == null) 
    { 
     System.out.println("ROW"+ mRowId); 
     long id = mDbHelper.createdetails(allergies, conditions, bloodgroup, note); 
     if (id > 0) 
     { 
      System.out.println("ROW"+ id); 
      mRowId = id; 

     } 

    } 
    else 
    { 
     mDbHelper.updatedetails(mRowId, allergies, conditions, bloodgroup, note); 
    } 
} 

@Override 
public final boolean onKeyDown(int keyCode, KeyEvent event) { 
    // TODO Auto-generated method stub 
    if (KeyEvent.KEYCODE_BACK == keyCode) 
    { 
     startActivity(new Intent(this, Menu.class)); 
     finish(); 
     onDestroy(); 
    } 
    return super.onKeyDown(keyCode, event); 

} 

} 
+0

System.out.println(「id is」+ mRowId);這行打印什麼? –

+0

首先檢查現有的帖子,這裏是一個int的解決方案,將其改爲浮動,你會很好http://stackoverflow.com/questions/7074097/how-to-pass-integer-from-one-activity-to - 另一個 – Xavjer

+0

你在mRowId中分配了什麼值?我想在populateFields函數中你必須分配this.mRowId = mRowId。 –

回答

0

sachi你好,請嘗試下面的代碼。

private void populateFields(int mRowId) 
{ 
    this.mRowId = mRowId; 
    Cursor profile = mDbHelper.fetchdetails(mRowId); 
    startManagingCursor(profile); 

    mAllergiesText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_ALLERGIES))); 
    mConditionsText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_CONDITIONS))); 
    mBloodText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_BLOOD))); 
    mNoteText.setText(profile.getString(profile 
      .getColumnIndexOrThrow(MyProfileDb.KEY_NOTE))); 

    profile.close(); 
} 

或者

你可以試試這個。

if (c.getCount() >= 1) 
    { 
     this.mRowId = c.getCount(); 
     populateFields(c.getCount()); 
     c.close(); 
    } 
else 
{ 
    this.mRowId = 0; 
} 
0

您提供populateFields中的行數而不是行ID。

相關問題