没有启动活动的android putextra
Posted
技术标签:
【中文标题】没有启动活动的android putextra【英文标题】:android putextra without start activity 【发布时间】:2012-08-15 08:20:15 【问题描述】:我需要帮助。 我有两个活动和一个分贝。我想要做的就是当我在活动 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;
@Override
public 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();
@Override
protected 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));
【问题讨论】:
所以你不想启动 Activity B 但你想从 A -> B 发送数据? 为什么代码不起作用? (您注释掉的内容看起来不错。)活动 B 会崩溃吗?如果是这样,请发布 logcat 错误。 no no no crash and that works but it works but if I start activity B and I won't do this I won't start B 您想在活动 A 中将InputString
添加到您的数据库吗?
是,但不启动活动 B
【参考方案1】:
如果我理解正确的话,您希望将数据从 A 发送到 B而不启动 B。在这种情况下,您需要忘记 Intents 并考虑在您的应用程序中使用store data 的方法。我的建议是使用SharedPreferences。
这里是你如何使用它:
在您的活动 A 中,存储字符串:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("string_id", InputString); //InputString: from the EditText
editor.commit();
在您的活动 B 中,您可以通过以下方式获取值:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String data = prefs.getString("string_id", "no id"); //no id: default value
【讨论】:
【参考方案2】:您的问题很难理解,但您似乎希望能够在不启动另一个 Activity 的情况下将 EditText 的内容添加到您的数据库中。只需在 Image 的 OnClickListener 中执行此操作:
public void onClick(View v)
NoteHelper2 helper2 = new NoteHelper2(this);
helper2.insert(MyInputText.getText().toString());
您还应该了解Java naming convention。
【讨论】:
以上是关于没有启动活动的android putextra的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio - 在没有启动器活动的情况下运行应用程序
Android:如何获取已安装活动的列表,就像它们出现在启动器中一样,没有重复