开发者

How to pass the RowId in android?

Here i am trying to pass my RowId to another activity, but it is printing null.In my myprofile class i am trying to pass the row id. Please any one help me.

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) {
    sw开发者_运维知识库itch (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);

}

}


Hi sachi Please try the below code .

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

Or

you can try this.

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


You feed the count of rows in populateFields and not the row id.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜