![](/img/trans.png)
[英]MainActivity to Fragment and from that Fragment back to MainActivity
[英]How to invoke info-getting from an Android fragment to the MainActivity from within the MainActivity?
因此,我试图围绕Android Fragments进行思考。 如果我在MainActivity中输入以下代码:
public void getMessage(Object obj) {
Log.wtf("My object: ", obj.toString());
}
以及我的片段中的以下代码:
(((NewNotificationRule)getActivity())。getMessage(“是的,哇!”);
我得到了“啊,哇!” 进入我的MainActivity。 事实是,这会将字符串从我的片段推到我的Activity,在此我希望它以其他方式起作用。 该片段仅定义了几个EditText,因此,在单击MainActivity调用的xml中定义的提交按钮时,我希望MainActivity在片段内提取EditTexts中定义的信息,以便它可以将其提交到DB中。 总结一下:我想从MainActivity中提取某些内容(准确地说是R.id.myEditText),而不是从片段中提取内容。
有什么方法可以将 EditText的内容从片段拉到 Activity中? 欢迎所有提示,因为我在这里完全迷路了。
一种快速的选择(不确定是否安全或建议这样做)是在项目中创建一个类,该类具有存储信息所需的属性,并在MainActivity中实例化该类的对象。 然后,从片段中引用它,并在文本被更改或引入片段的EditText中时,填充需要保存的数据(例如,在属性EditText1Data之内)。 然后,只需将包含Fragment信息的对象中包含的数据存储到DB中即可。 在此调用的类的构造函数中的属性中放置一些默认值,以避免出现空值问题。 这可以帮助您轻松地在两个方向上传输Activity <-> Fragments,即使这可能意味着您必须非常小心,因为您可能会得到null指针异常。
//这是您的DataClass,用于在Activity和Fragment之间传输数据。
public class DataClass {
public String EditText1Value;
public String EditText2Value;
public DataManager()
{
EditText1Value="Default Text";
EditText2Value="Default Text";
}
}
//这是MainActivityClass
public class MainActivity extends Activity{
//instance of the DataClass to be passed to fragments with the method getDataClass
public DataClass dataClass = new DataClass();
//Main Activity code goes here...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
}
//This method returns a reference to the DataClass Object
public DataClass getDataClass()
{
//Return this class instance object of the DataClass class
return (dataClass);
}
//Now this is the method to push data to DB, called whenever an activity button is pressed.
private boolean WriteToDB ()
{
//Suppose this receives a String
WritetoDB(dataClass.EditText1Value);
}
}
//这是通过DataClass对象发送数据的Fragment
public class ExampleFragment extends Fragment {
//Used to reference MainActivityObject and store info
DataClass dataClass;
//Used to Reference Activity's EditTexts
private EditText editText1;
//TextWatcher used to detect the EditText Field Changes
private TextWatcher EditText1_Txtwtr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.whatever_layout, container, false);
editText1= (EditText)v.findViewById(R.id.idEditText1);
setHasOptionsMenu(true);
return v;
}
@Override
public void onResume ()
{
super.onResume();
//code...
//Get MainActivity's DataClass object reference.
dataClass= ((MainActivity)getActivity()).getDataClass();
//store info whenever you need to, not necessarily on each keystroke, and store it in the object, not in the DB
dataClass.EditText1Value = editText1.getText().toString();
// Also, to capture whenever a edittext changes, you can use a textwatcher.
EditText1_Txtwtr= new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{}
@Override
public void afterTextChanged(Editable editable)
{}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
dataClass.EditText1Value = editText1.getText().toString();
}
}
//Asign TextWatcher to your Edit Text.
editText1.addTextChangedListener(EditText1_Txtwtr);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.