【问题标题】:In fragment, edittext to listview, data delivery在fragment中,edittext到listview,数据传递
【发布时间】:2014-07-23 00:46:57
【问题描述】:

我有一个项目扩展片段

现在,我的项目有三个选项卡。 是作曲片段。

写!!!!在第一个选项卡中编辑文本 → 推送!!!!添加按钮→查看!!!!第二个标签中的列表视图

请..帮帮我!!

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;

【问题讨论】:

    标签: android listview tabs fragment


    【解决方案1】:

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

    这里是从片段回调到活动的示例代码。 您可以在http://developer.android.com/training/basics/fragments/communicating.html中找到更多详细代码

    第一个片段

    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
            }
        }
    }
    

    【讨论】:

    • 我试试这个代码。但它失败了......我想向你发送我的项目的电子邮件......我可以问你的电子邮件吗?
    • 我想显示错误日志,但我的项目有很多类。我在互联网上下载了这个项目文件。所以我很难理解这个项目..
    猜你喜欢
    • 2015-02-03
    • 2021-02-19
    • 1970-01-01
    • 2021-05-11
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多