如何从MainActivity中调用从Android片段到MainActivity的信息获取?

时间:2013-10-04 17:02:49

标签: java android android-fragments

所以我试图围绕Android Fragments。如果我将以下代码放在我的MainActivity中:

public void getMessage(Object obj) {
    Log.wtf("My object: ", obj.toString());
}

以及我片段中的以下代码:

((NewNotificationRule)getActivity())。getMessage(“Yah wohooo!”);

我得到了“Yah wohooo!”进入我的MainActivity。问题是,这会将我的片段中的字符串推送到我的Activity,我想让它以相反的方式工作。片段只定义了几个EditTexts,因此在点击MainActivity调用的xml中定义的submit-button时,我希望MainActivity拉出片段内EditTexts中定义的信息,以便它可以将它提交到DB中。总而言之:我想从我的MainActivity中提取一些东西(R.id.myEditText来精确),而不是从片段中推送它。

有什么方法可以从一个片段中的EditText的内容拉到一个Activity中?欢迎所有提示,因为我完全迷失在这里..

1 个答案:

答案 0 :(得分:0)

一个快速选项(不确定这是安全的还是推荐的)是在项目中创建一个类,其中包含在MainActivity中存储信息和实例化此类对象所需的属性。然后,每当文本被更改或引入片段的EditText时,从片段中引用它并填充您需要保存的数据(例如,在属性EditText1Data或其他内容中)。然后只需将您填充的片段信息中包含的数据存储到数据库中。将一些默认值放在此被调用类的构造函数中的atttributes中,以避免出现null stuff问题。这可以帮助您轻松地在两个方向上传输数据Activity< - > Fragments,即使这可能意味着您必须非常小心,因为您可以获得空指针异常。

//这是您的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对象发送数据的片段

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

    }




}