你可以看到我尝试过putExtras,putSerializable但是无法让它工作.我想我错过了一些理解.
因此,对于我在下面的活动中的配置文件菜单选项,我可以将光标行ID的值发送到Profile类:
public class Overthelimit extends ListActivity {private OverlimitDbAdapter dbHelper;private Cursor cursor;/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); this.getListVIEw(); dbHelper = new OverlimitDbAdapter(this); dbHelper.open(); fillData(); registerForContextMenu(getListVIEw());}@OverrIDeprotected 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);}@OverrIDeprotected voID onDestroy() { super.onDestroy(); if (dbHelper != null) { dbHelper.close(); }}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu,menu); return true;} @OverrIDepublic 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);}}
然后在我的个人资料活动中将其作为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; }
然后,我需要将此mRowID用于MyUsual中另一个名为DrinkList的活动.所以我在下面的MyUsual上使用了onlinner上的drink1按钮来尝试将行ID发送到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. */@OverrIDepublic 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); } }
从DrinksList我选择一个饮料,我需要使用mRowID通过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; } } }
对于长篇文章感到抱歉,但我需要做的就是让mRowID的这个值可用于所有活动,这样我就可以在任何时候读/写数据.如果应用程序通过拨打来电暂停或中断,数据也需要在那里,所以我使用onSaveInstanceState.
好,谢谢.所以回复很好的答案,我已经完成了这个,但它崩溃试图获取数据.我将此作为我的Application类:
public class OverthelimitApplication extends Application {private Long rowID;public Long getRowID() { return rowID;}public voID setRowID(Long value) { rowID = value;}}
然后用这个设置值:
OverthelimitApplication app1 = (OverthelimitApplication)getApplicationContext(); app1.setRowID((long) cursor.getColumnIndexOrThrow(OverlimitDbAdapter.KEY_ROWID));
然后试着用这个获得价值并且它崩溃了:
mRowID = ((OverthelimitApplication) getApplicationContext()).getRowID();
我修好了!使用这个集合得到:
app1.setRowID(Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(OverlimitDbAdapter.KEY_ROWID))));mRowID = (long)((OverthelimitApplication)getApplicationContext()).getRowID();
设置和获取时我仍然必须指定长时间.感谢您的输入.
解决方法 另一种方法是创建一个可用于所有活动的应用程序类.要做到这一点,你必须扩展你的Manifest
<application .. androID:name=".MyApplication" >
并创建一个新类
public class MyApplication extends Application {public int rowID = 0;}
在活动内部,您可以访问rowID
int mRowID = ((MyApplication) getApplicationContext()).rowID;总结
以上是内存溢出为你收集整理的在所有Android活动中保持变量值全部内容,希望文章能够帮你解决在所有Android活动中保持变量值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)