如何在android中传递RowId?

时间:2011-10-04 05:41:29

标签: android database

这里我试图将我的RowId传递给另一个活动,但它正在打印null。在我的myprofile类中,我试图传递行ID。请任何人帮助我。

myprofile class

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 class

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);

}

}

2 个答案:

答案 0 :(得分:0)

Hi 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;
}

答案 1 :(得分:0)

您可以在populateFields中输入行数,而不是行ID。