Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中传递RowId?_Android_Database - Fatal编程技术网

如何在android中传递RowId?

如何在android中传递RowId?,android,database,Android,Database,在这里,我试图将我的行id传递给另一个活动,但它正在打印null。在我的myprofile类中,我试图传递行id。请任何人帮助我 myprofile类 public class MyProfile extends Activity implements OnClickListener { private MyProfileDb mDbHelper; private TextView mAllergiesText; private TextView mConditionsText; priv

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

myprofile类

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

}

}

嗨,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;
}

您输入的是populateFields中的行数,而不是行id。

System.out.printlnid是+mRowId;这行打印的是什么?首先检查现有的帖子,这里有一个int的解决方案,把它改成float,你就可以知道你在mRowId中指定了什么值了?我认为在populateFields函数中,您必须分配this.mRowId=mRowId。是的,它打印空值,因为您没有在mRowId中分配任何内容。请阅读我的第二条评论。