在片段中,edittext到listview,数据传递

时间:2014-07-23 00:46:57

标签: android listview tabs fragment

  

我有一个项目扩展片段

现在,我的项目有三个标签。 它是组成片段。

写入!!!!第一个标签中的edittext→推!!!!添加按钮→查看!!!!第二个标签中的列表视图

请帮助我!!  

final Button btn1 = (Button) view.findViewById(R.id.ButtonAdd);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {            
  String str = mEditMAC.getText().toString();       
   });

public View getView(int position, View convertView, ViewGroup parent) { return parent;

1 个答案:

答案 0 :(得分:0)

片段之间的基本通信是 片段 - >活动 - >另一个片段

以下是从片段到活动的回调示例代码。 您可以在http://developer.android.com/training/basics/fragments/communicating.html

中找到更多详细信息代码

FirstFragment

public class FirstFragment extends Fragment {
    public interface OnBtnClickListener {
        public void buttonClicked(String mac);
    }
    OnBtnClickListener mCallback;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnBtnClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement OnBtnClickListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // initialize
        // ...

        final Button btn1 = (Button) view.findViewById(R.id.ButtonAdd);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {            
                String str = mEditMAC.getText().toString();
                mCallback.buttonClicked(str);
            }      
        });
     }
}

活动

public class YourActivity extends Activity implements FirstFragment.OnBtnClickListener {
    @Override
    public void buttonClicked(String mac) {
        // find second fragment and call the proper function
        SecondFragment fragment = (SecondFragment)getSupportFragmentManager().findFragmentByTag(SecondFragment.TAG);
        if (fragment != null) {
            fragment.buttonClicked(mac); // update second fragment's listview
        }
    }
}