
我需要帮助.
我有两个活动和一个数据库.我想要做的就是当我按下活动A中的按钮时,我将我的editText中的字符串发送到活动B.在活动B中,我尝试在我的数据库中插入该字符串并将其显示在列表视图中.如果我在活动A中启动活动B但是我不会显示活动B.
这是我的代码:
活动A:
public class Example extends Activity {public final static String ID_EXTRA2="com.example.activitIEs";public EditText MyinputText = null;ImageVIEw Mybutton;TextVIEw MyOutputText, Myinputtext; /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.diccionary);MyinputText = (EditText)findVIEwByID(R.ID.inputText);MyOutputText = (TextVIEw)findVIEwByID(R.ID.OutputText); Mybutton = (ImageVIEw)findVIEwByID(R.ID.botonaceptar); Mybutton.setonClickListener(MybuttonOnClickListener); public ImageVIEw.OnClickListener MyTranslatebuttonOnClickListener = new ImageVIEw.OnClickListener(){ public voID onClick(VIEw v) { String inputString; inputString = MyinputText.getText().toString(); try{ Intent d = new Intent(Example.this, Secondactivity.class); d.putExtra(ID_EXTRA2, inputString); //startActivity(d); <----- If i start the secondactivity, that works... }catch (Exception e){ }}};public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); MenuInflater awesome = getMenuInflater(); awesome.inflate(R.menu.main_menu, menu); return true;}public boolean onoptionsItemSelected(MenuItem item){ switch(item.getItemID()){ case R.ID.menu1: startActivity(new Intent("com.example.activitIEs.SECONDACTIVITY")); return true; case R.ID.menu2: //something to do return true;} return false;}};活动B:
public class Secondactivity extends Activity {NoteAdapter2 adapter = null;NoteHelper2 helper2 = null;Cursor dataset_cursor2 = null;String entriID = null;public ListVIEw List2;String passeDWord = null;@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.second); try{ List2 = (ListVIEw)findVIEwByID(R.ID.List2); helper2 = new NoteHelper2(this); ---->passeDWord = getIntent().getStringExtra(Example.ID_EXTRA2); ---->helper2.insert(passeDWord); //here i insert the string in my db startManagingCursor(dataset_cursor2); dataset_cursor2 = helper2.getAll(); adapter = new NoteAdapter2(dataset_cursor2); List2.setAdapter(adapter); dataset_cursor2.requery(); adapter.notifyDataSetChanged(); //this is how we kNow to do when a List item is clicked List2.setonItemClickListener(onListClick); } catch (Exception e){ // this is the line of code that sends a real error message to the log Log.e("ERROR", "ERROR IN CODE" + e.toString()); //this is the line that prints out the location in the code where the error ocurred e.printstacktrace(); }}@OverrIDeprotected voID onDestroy() { super.onDestroy(); helper2.close();}private AdapterVIEw.OnItemClickListener onListClick = new AdapterVIEw.OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID){ entriID = String.valueOf(ID); }};class NoteAdapter2 extends CursorAdapter { NoteAdapter2(Cursor c){ super(Zhistorytranslate.this, c); } public voID bindVIEw(VIEw row2, Context ctxt, Cursor c) { NoteHolder2 holder2 = (NoteHolder2)row2.getTag(); holder2.populateFrom2(c, helper2); } public VIEw newVIEw(Context ctxt, Cursor c, VIEwGroup parent) { LayoutInflater inflater = getLayoutInflater(); VIEw row2 = inflater.inflate(R.layout.row2, parent, false); NoteHolder2 holder2 = new NoteHolder2(row2); row2.setTag(holder2); return (row2); } }static class NoteHolder2 { private TextVIEw entriText = null; NoteHolder2(VIEw row2){ entriText = (TextVIEw)row2.findVIEwByID(R.ID.entri); } voID populateFrom2(Cursor c, NoteHelper2 helper2) { entriText.setText(helper2.getEntri(c)); }}解决方法:
如果我理解正确,你想要在不启动B的情况下将数据从A发送到B.在这种情况下,你需要忘记Intents并考虑在你的应用程序中使用store data的方法.我的建议是使用SharedPreferences.
以下是如何使用它:
>在您的活动A中,存储字符串:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor editor = prefs.edit();editor.putString("string_ID", inputString); //inputString: from the EditTexteditor.commit();>在活动B中,您可以通过以下方式获取值:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);String data = prefs.getString("string_ID", "no ID"); //no ID: default value 总结 以上是内存溢出为你收集整理的没有启动活动的android putextra全部内容,希望文章能够帮你解决没有启动活动的android putextra所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)