保留所有的android活动的变量值有的、变量值、android

2023-09-12 04:29:27 作者:Perfunctory 敷衍

我有一行数据将在多个活动中使用的数据库。我需要能够保持行ID提供一切活动都让我可以阅读和我的数据库适配器写在不同的activites数据。我已经成功地使用putExtra(Overthelimit.java)通过一个意图行ID传递到下一个活动。那么mRowId变量给出的行ID使用getExtra(Profile.java)。这个问题我现在已经是可用于其他活动mRowId即MyUsual和DrinksList,所以我可以,我去更新数据。

您可以看到我已经试过putExtras,putSerializable但不能让它开始工作。我想我错过了一些了解。

因此​​,对于下面的活动我的个人资料菜单选项,我可以把光标行id的值,以文件类:

 公共类Overthelimit扩展ListActivity {
私人OverLimitDbAdapter dbHelper;
私人光标指针;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    this.getListView();
    dbHelper =新OverLimitDbAdapter(本);
    dbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}


@覆盖
保护无效onActivityResult(INT申请code,INT结果code,
        意向意图){
    super.onActivityResult(要求code,因此code,意图);
    fillData();

}
私人无效fillData(){
    光标= dbHelper.fetchAllUserDrinks();
    startManagingCursor(光标);
    //cursor.getCount();

    的String []从=新的String [] {OverLimitDbAdapter.KEY_USERNAME};
    INT []到=新INT [] {R.id.label};

    //现在创建一个数组适配器,并将其设置为显示用我们的行
    SimpleCursorAdapter注意到=新SimpleCursorAdapter(这一点,
            R.layout.user_row,光标,从,到);
    setListAdapter(票据);
}



@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    如果(dbHelper!= NULL){
        dbHelper.close();
    }
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_menu,菜单);
    返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理项目选择
    开关(item.getItemId()){
    案例R.id.profile:
        意图myIntent1 =新的意图(这一点,Profile.class);
        如果(cursor.getCount()!= 0){
            //Toast.makeText(getApplicationContext(),没有配置文件,Toast.LENGTH_SHORT).show();
            myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID,cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
        }
        startActivityForResult(myIntent1,0);
        返回true;
    案例R.id.myusual:
        意图myIntent2 =新的意图(这一点,MyUsual.class);
        startActivityForResult(myIntent2,0);
        返回true;
    案例R.id.trackme:
        意图myIntent3 =新的意图(这一点,TrackMe.class);
        startActivityForResult(myIntent3,0);
        返回true;
    案例R.id.moreinfo:
        意图myIntent4 =新的意图(这一点,MoreInfo.class);
        startActivityForResult(myIntent4,0);
        返回true;


    }
    返回super.onOptionsItemSelected(项目);
}

}
 

然后使其可作为mRowId下面我的个人资料活动:

  mRowId =(丛== NULL)?空值 :
                (龙)bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
             如果(mRowId == NULL){
                捆绑额外= getIntent()getExtras()。
                mRowId =演员!= NULL?的Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                        : 空值;
            }
 
Appium入门 1 Windows环境搭建

然后我需要让这个mRowId提供给所谓的DrinkList从MyUsual另一项活动。所以我下面有一个drink1按钮onClickListener MyUsual尝试发送行ID来DrinksList:

 公共类MyUsual延伸活动{
私人龙mRowId;
私人OverLimitDbAdapter mDbHelper;
私人光标指针;
私人TextView的mDrink1Label;
私人TextView的mDrink1Units;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(最终束束){
    super.onCreate(包);
    mDbHelper =新OverLimitDbAdapter(本);
    mDbHelper.open();
    的setContentView(R.layout.my_usual);
    mDrink1Label =(TextView中)findViewById(R.id.drink1Label);
    mDrink1Units =(TextView中)findViewById(R.id.drink1Units);




    按钮drink1 =(按钮)findViewById(R.id.drink1Button);

    //获取数据的意图,即这饮按钮pressed和mRowId
          mRowId =(丛== NULL)?空值 :
            (龙)bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
         如果(mRowId == NULL){
            捆绑额外= getIntent()getExtras()。
            mRowId =演员!= NULL?的Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                    : 空值;
        }

        // populateFields();

        drink1.setOnClickListener(新View.OnClickListener(){
         公共无效的onClick(视图查看){
             的setResult(RESULT_OK);
                //完();
                意图myIntent1 =新的意图(view.getContext(),DrinksList.class);
                myIntent1.putExtra(drinkBut​​ton,drink1);

                如果(cursor.getCount()!= 0){
                    myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID,cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
                }

             startActivityForResult(myIntent1,0);
         }

        });




}

保护无效的onSaveInstanceState(包outState){
            super.onSaveInstanceState(outState);
            // saveState和();
            outState.putSerializable(OverLimitDbAdapter.KEY_ROWID,mRowId);
        }
}
 

从DrinksList我选择喝一杯,我需要使用mRowId将数据写入到通过onListItemclick数据库:

 公共类DrinksList扩展ListActivity {
私人ProgressDialog m_ProgressDialog = NULL;
私人的ArrayList< CreateDrinkOption> m_drinks = NULL;
私人DrinkAdapter m_adapter;
私人的Runnable viewDrinks;
私人字符串drinkBut​​ton;
私人龙mRowId;
私人OverLimitDbAdapter mDbHelper;
私人字符串databaseRow;
私人光标指针;

/ **第一次创建活动时调用。 * /

@覆盖
 公共无效的onCreate(束捆){
     super.onCreate(包);
     的setContentView(R.layout.drinks_list);
     mDbHelper =新OverLimitDbAdapter(本);
     mDbHelper.open();
     m_drinks =新的ArrayList< CreateDrinkOption>();
     this.m_adapter =新DrinkAdapter(这一点,R.layout.drink_row,m_drinks);
             setListAdapter(this.m_adapter);


     viewDrinks =新的Runnable(){
         @覆盖
         公共无效的run(){
             getDrinks();
         }
     };
 线程线程=新主题(空,viewDrinks,MagentoBackground);
     thread.start();
     m_ProgressDialog = ProgressDialog.show(DrinksList.this,
           请稍候...,检索数据...,真正的);



//获取数据的意图,即这饮按钮pressed和mRowId
     mRowId =(丛== NULL)?空值 :
        (龙)bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
     如果(mRowId == NULL){
        捆绑额外= getIntent()getExtras()。
        drinkBut​​ton = extras.getString(drinkBut​​ton);
        mRowId =演员!= NULL?的Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                : 空值;
    }


 }

保护无效的onSaveInstanceState(包outState){
    super.onSaveInstanceState(outState);
    // saveState和();
    outState.putSerializable(OverLimitDbAdapter.KEY_ROWID,mRowId);
}


 私人可运行returnRes =新的Runnable(){

     @覆盖
      公共无效的run(){
          如果(m_drinks =空&安培;!&安培; m_drinks.size()大于0){
              m_adapter.notifyDataSetChanged();
              的for(int i = 0; I< m_drinks.size();我++)
              m_adapter.add(m_drinks.get(ⅰ));
          }
          m_ProgressDialog.dismiss();
          m_adapter.notifyDataSetChanged();
      }
    };

    @覆盖
    保护无效onListItemClick(ListView的L,视图V,INT位置,长ID)
    {
    尝试
    {
    super.onListItemClick(L,V,位置ID);
    CreateDrinkOption BKG =(CreateDrinkOption)l.getItemAtPosition(位置);
    。字符串drink1type = bkg.getDrinkType()的toString();
    浮drink1units =(bkg.getPercentageByVolume()* bkg.getVolume());
    //Toast.makeText(this,mRowId.toString(),Toast.LENGTH_LONG).show();

    mDbHelper.updateDrink(mRowId,drink1type,drink1units);
    完();

    }
    赶上(例外前)
    {
    Toast.makeText(这一点,错误,Toast.LENGTH_LONG).show();
    }

   }



 私人无效getDrinks(){
     尝试{
         m_drinks =新的ArrayList< CreateDrinkOption>();
         CreateDrinkOption 01 =新CreateDrinkOption();
         o1.setDrinkType(啤酒 -  1品脱);
         o1.setPercentageByVolume((浮点)4.5);
         o1.setVolume((浮点)0.5);
         m_drinks.add(01);
         CreateDrinkOption O2 =新CreateDrinkOption();
         o2.setDrinkType(葡萄酒 - 小杯);
         o2.setPercentageByVolume((浮点)12);
         o2.setVolume((浮点)0.125);
         m_drinks.add(02);
         CreateDrinkOption O3 =新CreateDrinkOption();
         o3.setDrinkType(精神 - 单);
         o3.setPercentageByVolume((浮点)40);
         o3.setVolume((浮点)0.25);
         m_drinks.add(03);
         CreateDrinkOption O4 =新CreateDrinkOption();
         o4.setDrinkType(ALCOPOP  - 瓶);
         o4.setPercentageByVolume((浮动)5);
         o4.setVolume((浮点)0.275);
         m_drinks.add(O4);
            视频下载(1000);
         Log.i(阵列,+ m_drinks.size());
       }赶上(例外五){
        Log.e(BACKGROUND_PROC,e.getMessage());
       }
       runOnUiThread(returnRes);
   }

 私有类DrinkAdapter扩展ArrayAdapter< CreateDrinkOption> {

     私人的ArrayList< CreateDrinkOption>项目;

     公共DrinkAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< CreateDrinkOption>项目){
              超(背景下,textViewResourceId,项目);
              this.items =项目;
      }

     @覆盖
      公共查看getView(INT位置,查看convertView,ViewGroup中父){
              视图V = convertView;
              如果(V == NULL){
                  LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  V = vi.inflate(R.layout.drink_row,NULL);
              }
              CreateDrinkOption O = items.get(位置);
              如果(O!= NULL){
                      TextView的TT =(TextView中)v.findViewById(R.id.drinkdetail);
                      TextView的BT =(TextView中)v.findViewById(R.id.drinkunits);
                      如果(TT!= NULL){
                            tt.setText(类型:+ o.getDrinkType());
                      }
                      如果(BT!= NULL){
                            bt.setText(单位:+将String.valueOf(o.getPercentageByVolume()* o.getVolume()));
                      }
              }
              返回伏;


    }

 }


}
 

很抱歉的长期职位,但我需要做的是使这个值mRowId适用于一切活动都让我可以在任何时候读/写数据。数据还需要存在,如果应用程序被暂停或者说来电interupted,所以我用的onSaveInstanceState。

好的,谢谢。所以回复伟大的答案,我已经做到了这一点,但它崩溃试图获取数据。我有这个作为我的应用程序类:

 公共类OverthelimitApplication扩展应用{
私人龙ROWID;
众长getRowId(){
    返回ROWID;
}
公共无效setRowId(Long值){
    ROWID =价值;
}
}
 

然后设置值是:

  OverthelimitApplication APP1 =(OverthelimitApplication)getApplicationContext();
    app1.setRowId((长)cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID));
 

然后尝试获得价值这一点,它崩溃了:

  mRowId =((OverthelimitApplication)getApplicationContext())getRowId()。
 

我有固定它!使用此设置和获取:

app1.setRowId(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)))); mRowId =(长)((OverthelimitApplication)getApplicationContext())getRowId()。

我仍然有指定长设置和获取时。感谢您的输入。

解决方案

另一种方法是创建一个适用于所有的活动应用程序类。 要做到这一点,你必须对你的清单扩展以

 <应用
    ..
    机器人:名称=。MyApplication的>
 

和创建一个新的类

 公共类MyApplication的扩展应用{
公众诠释ROWID = 0;
}
 

在活动里面,你可以通过

访问ROWID

  INT mRowId =((所有MyApplication)getApplicationContext())ROWID。
 

I have a database with one row of data that will be used across a number of activities. I need to be able to keep the row id available in all activites so I can read and write data across different activites with my DB adapter. I have successfully used putExtra (Overthelimit.java) via an intent to pass a row id to the next activity. mRowId variable is then given the row id using getExtra (Profile.java). The problem I now have is making mRowId available to other activities i.e. MyUsual and DrinksList so I can update data as I go.

You can see I have tried putExtras, putSerializable but can't get it to work. I think I am missing some understanding.

So for my profile menu option in the activity below I can send the value of the cursor row id to Profile class:

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

    // Now create an array adapter and set it to display using our row
    SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
            R.layout.user_row, cursor, from, to);
    setListAdapter(notes);
}



@Override
protected void onDestroy() {
    super.onDestroy();
    if (dbHelper != null) {
        dbHelper.close();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
} 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.profile:
        Intent myIntent1 = new Intent(this, Profile.class);
        if(cursor.getCount() != 0) {
            //Toast.makeText(getApplicationContext(), "no profile",Toast.LENGTH_SHORT).show();
            myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID, cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
        }
        startActivityForResult(myIntent1, 0);
        return true;
    case R.id.myusual:
        Intent myIntent2 = new Intent(this, MyUsual.class);
        startActivityForResult(myIntent2, 0);
        return true;
    case R.id.trackme:
        Intent myIntent3 = new Intent(this, TrackMe.class);
        startActivityForResult(myIntent3, 0);
        return true;
    case R.id.moreinfo:
        Intent myIntent4 = new Intent(this, MoreInfo.class);
        startActivityForResult(myIntent4, 0);
        return true;


    }
    return super.onOptionsItemSelected(item);
}

}

Then make it available as mRowId in my Profile activity below:

mRowId = (bundle == null) ? null :
                (Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
             if (mRowId == null) {
                Bundle extras = getIntent().getExtras();
                mRowId = extras != null ? Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                        : null;
            }

I then need to make this mRowId available to another activity called DrinkList from MyUsual. so I have MyUsual below with a drink1 button onClickListener to try and send the row id to DrinksList:

public class MyUsual extends Activity {
private Long mRowId;
private OverLimitDbAdapter mDbHelper;
private Cursor cursor;
private TextView mDrink1Label;
private TextView mDrink1Units;

/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle bundle) {
    super.onCreate(bundle);
    mDbHelper = new OverLimitDbAdapter(this);
    mDbHelper.open();
    setContentView(R.layout.my_usual);
    mDrink1Label = (TextView) findViewById(R.id.drink1Label);
    mDrink1Units = (TextView) findViewById(R.id.drink1Units);




    Button drink1 = (Button) findViewById(R.id.drink1Button);

    // get intent data i.e. which drink button pressed and mRowId                
          mRowId = (bundle == null) ? null :
            (Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
         if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                    : null;
        }           

        //populateFields();

        drink1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {   
             setResult(RESULT_OK);
                //finish();
                Intent myIntent1 = new Intent(view.getContext(), DrinksList.class);
                myIntent1.putExtra("drinkButton", "drink1");

                if(cursor.getCount() != 0) {
                    myIntent1.putExtra(OverLimitDbAdapter.KEY_ROWID, cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID)));
                }

             startActivityForResult(myIntent1, 0);
         }

        });




}

protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            //saveState();
            outState.putSerializable(OverLimitDbAdapter.KEY_ROWID, mRowId);
        }   
}

From DrinksList I select a drink and I need to use the mRowId write the data to the database via the onListItemclick:

public class DrinksList extends ListActivity {
private ProgressDialog m_ProgressDialog = null; 
private ArrayList<CreateDrinkOption> m_drinks = null;
private DrinkAdapter m_adapter;
private Runnable viewDrinks;
private String drinkButton;
private Long mRowId;
private OverLimitDbAdapter mDbHelper;
private String databaseRow;
private Cursor cursor;

/** Called when the activity is first created. */

@Override
 public void onCreate(Bundle bundle) {
     super.onCreate(bundle);
     setContentView(R.layout.drinks_list);
     mDbHelper = new OverLimitDbAdapter(this);
     mDbHelper.open();
     m_drinks = new ArrayList<CreateDrinkOption>();
     this.m_adapter = new DrinkAdapter(this, R.layout.drink_row, m_drinks);
             setListAdapter(this.m_adapter);


     viewDrinks = new Runnable(){
         @Override
         public void run() {
             getDrinks();
         }
     };
 Thread thread =  new Thread(null, viewDrinks, "MagentoBackground");
     thread.start();
     m_ProgressDialog = ProgressDialog.show(DrinksList.this,    
           "Please wait...", "Retrieving data ...", true);



// get intent data i.e. which drink button pressed and mRowId                
     mRowId = (bundle == null) ? null :
        (Long) bundle.getSerializable(OverLimitDbAdapter.KEY_ROWID);
     if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        drinkButton = extras.getString(drinkButton);
        mRowId = extras != null ? Long.parseLong(extras.getString(OverLimitDbAdapter.KEY_ROWID))
                                : null;
    }


 }

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //saveState();
    outState.putSerializable(OverLimitDbAdapter.KEY_ROWID, mRowId);
}


 private Runnable returnRes = new Runnable() {

     @Override
      public void run() {
          if(m_drinks != null && m_drinks.size() > 0){
              m_adapter.notifyDataSetChanged();
              for(int i=0;i<m_drinks.size();i++)
              m_adapter.add(m_drinks.get(i));
          }
          m_ProgressDialog.dismiss();
          m_adapter.notifyDataSetChanged();
      }
    };

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
    try
    {
    super.onListItemClick(l, v, position, id);
    CreateDrinkOption bkg = (CreateDrinkOption)l.getItemAtPosition(position);
    String drink1type = bkg.getDrinkType().toString();
    float drink1units = (bkg.getPercentageByVolume() * bkg.getVolume());
    //Toast.makeText(this, mRowId.toString(), Toast.LENGTH_LONG).show();

    mDbHelper.updateDrink(mRowId, drink1type, drink1units); 
    finish();

    }
    catch(Exception ex)
    {
    Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
    }

   }



 private void getDrinks(){
     try{
         m_drinks = new ArrayList<CreateDrinkOption>();
         CreateDrinkOption o1 = new CreateDrinkOption();
         o1.setDrinkType("Beer - 1 pint");
         o1.setPercentageByVolume((float) 4.5);
         o1.setVolume((float) 0.5);
         m_drinks.add(o1);
         CreateDrinkOption o2 = new CreateDrinkOption();
         o2.setDrinkType("Wine - small glass");
         o2.setPercentageByVolume((float) 12);
         o2.setVolume((float) 0.125);
         m_drinks.add(o2);
         CreateDrinkOption o3 = new CreateDrinkOption();
         o3.setDrinkType("Spirit - single");
         o3.setPercentageByVolume((float) 40);
         o3.setVolume((float) 0.25);
         m_drinks.add(o3);
         CreateDrinkOption o4 = new CreateDrinkOption();
         o4.setDrinkType("Alcopop - bottle");
         o4.setPercentageByVolume((float) 5);
         o4.setVolume((float) 0.275);
         m_drinks.add(o4);
            Thread.sleep(1000);
         Log.i("ARRAY", ""+ m_drinks.size());
       } catch (Exception e) { 
        Log.e("BACKGROUND_PROC", e.getMessage());
       }
       runOnUiThread(returnRes);
   }   

 private class DrinkAdapter extends ArrayAdapter<CreateDrinkOption> {

     private ArrayList<CreateDrinkOption> items;

     public DrinkAdapter(Context context, int textViewResourceId, ArrayList<CreateDrinkOption> items) {
              super(context, textViewResourceId, items);
              this.items = items;
      }

     @Override
      public View getView(int position, View convertView, ViewGroup parent) {
              View v = convertView;
              if (v == null) {
                  LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  v = vi.inflate(R.layout.drink_row, null);
              }
              CreateDrinkOption o = items.get(position);
              if (o != null) {
                      TextView tt = (TextView) v.findViewById(R.id.drinkdetail);
                      TextView bt = (TextView) v.findViewById(R.id.drinkunits);
                      if (tt != null) {
                            tt.setText("Type: "+o.getDrinkType());
                      }
                      if(bt != null){
                            bt.setText("Units: "+ String.valueOf(o.getPercentageByVolume() * o.getVolume()));
                      }
              }
              return v;


    }

 } 


}

Sorry for the long post, but all I need to do is make this value for mRowId available to all activites so I can read/write data at any point. The data also needs to be there if the app is paused or interupted by say an incoming call, so I use onSaveInstanceState.

ok, thanks. So reply to great answers and I have done this, but it crashes trying to get the data. I have this as my Application class:

public class OverthelimitApplication extends Application {
private Long rowId;
public Long getRowId() {
    return rowId;
}
public void setRowId(Long value) {
    rowId = value;
}
}

then set value with this:

OverthelimitApplication app1 = (OverthelimitApplication)getApplicationContext();
    app1.setRowId((long) cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID));

then try to get value with this and it crashes:

mRowId = ((OverthelimitApplication) getApplicationContext()).getRowId(); 

I have fixed it! using this the set and get:

app1.setRowId(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(OverLimitDbAdapter.KEY_ROWID))));

mRowId = (long)((OverthelimitApplication)getApplicationContext()).getRowId(); 

I still had to specify long when setting and getting. Thanks for all your input.

解决方案

Another way is to create a application class which is available for all activities. To do that, you have to extend you Manifest with

 <application
    ..
    android:name=".MyApplication" >

and create a new Class

public class MyApplication extends Application {
public int rowId = 0;
}

inside the activities, you can access the rowId by

int mRowId = ((MyApplication) getApplicationContext()).rowId;