小编use*_*784的帖子

在所有Android活动中保持变量值

我有一个数据库,其中包含一行数据,可用于多个活动.我需要能够在所有活动中保留行ID,以便我可以使用我的数据库适配器在不同的活动中读取和写入数据.我已成功使用putExtra(Overthelimit.java)通过意图将行id传递给下一个活动.然后使用getExtra(Profile.java)为mRowId变量赋予行id.我现在的问题是让mRowId可用于其他活动,即MyUsual和DrinksList,这样我就可以随时更新数据.

你可以看到我尝试过putExtras,putSerializable但是无法让它工作.我想我错过了一些理解.

因此,对于我在下面的活动中的配置文件菜单选项,我可以将光标行id的值发送到Profile类:

 public class Overthelimit extends ListActivity {
private OverLimitDbAdapter dbHelper;
private Cursor cursor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.getListView();
    dbHelper = new OverLimitDbAdapter(this);
    dbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}


@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();

}
private void fillData() {
    cursor = dbHelper.fetchAllUserDrinks();
    startManagingCursor(cursor);
    //cursor.getCount();    

    String[] from = new String[] { OverLimitDbAdapter.KEY_USERNAME };
    int[] to = new int[] { R.id.label …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-activity

7
推荐指数
2
解决办法
3万
查看次数

putExtras多个putString不起作用

我需要将2个变量从一个活动传递到另一个活动.

第一项活动我有以下内容:

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    Bundle bundle=new Bundle();
switch (item.getItemId()){ 

case 1: 

    bundle.putString(drinkButton, "4");
    bundle.putString(drinkType, "1");
    Intent myIntent1 = new Intent(this, DrinksList.class);
    myIntent1.putExtras(bundle);
    startActivityForResult(myIntent1, 0);
    return true;

case 2: 

    bundle.putString(drinkButton, "1");
    bundle.putString(drinkType, "2");
    Intent myIntent2 = new Intent(this, DrinksList.class);
    myIntent2.putExtras(bundle);
    startActivityForResult(myIntent2, 0);
    return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)

然后在第二个活动中我使用它来获取值,但两个值都是相同的,即与'drinkType'相同的情况1我得到"1"两个和情况2我得到"2"两个我期望得到4,1和1,2.

Bundle extras = getIntent().getExtras();

        drinkButton = extras.getString(drinkButton);


        drinkType = extras.getString(drinkType);

    Toast.makeText(this, "drink Button = "+drinkButton+"  Drink Type = "+drinkType, Toast.LENGTH_LONG).show();  


} 
Run Code Online (Sandbox Code Playgroud)

看来我不能超过一个额外的.有任何想法吗?

eclipse android extras

2
推荐指数
1
解决办法
4433
查看次数