简体   繁体   English

如何通过同一活动将从一个片段的列表视图选择的字符串传输到另一个片段的autocompleteTextview

[英]How to transfer string selected from listview of one fragment to autocompleteTextview of another fragment over the same activity

I have a listview on one fragment which consists of all the buses coming to a particular bus stop as listitems. 我在一个片段上有一个listview,该片段由作为特定项目进入特定公交车站的所有公共汽车组成。 I want when user click on any route no. 我想当用户单击任何路线编号时。 listed in the listview, to get transferred to the autocomplete textview on another fragment on the same activity with the same value copied to autocompletetextview which he selected from the list in first fragment. 列表视图中列出的内容,以将其转移到同一活动的另一个片段的自动完成文本视图中,并将相同的值复制到他从第一个片段的列表中选择的自动完成文本视图中。 On 2nd fragment user can see the detailed journey of that particular bus no. 在第二个片段上,用户可以看到该特定公交车号的详细行程。 which he selected before 他之前选择的

            write this in firstFragment listView itemclick;
            @Override
                public void onItemClick(Items item) {
                    String busnumber =  item.getBusNumber();
                    SecondFragment sFragment = new SecondFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("bNumber", busnumber);
                    sFragment.setArguments(bundle);
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.replace(R.id.activity_container_id, fragment, fragment.getClass().getSimpleName());
                    ft.commit();

                }

            than on second fragment in onCreate method get the bus number or string value you passed as bundle argument from first fragment and set it to autocomplete text.
        so second fragment something look like this: 

   onCreate(){
            Bundle bundle = this.getArguments();
            String bNumber;
            if (bundle != null) {
                bNumber = bundle.getString("bNumber", "");
            }

          AutoCompleteTextView actv= 
         (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
         actv.setText(bNumber);
    }

Most efficiently you can do is, create an Interface with function like onRouteSelected() then implement that Interface in FragmentOne and pass the FragmentOne reference to you adapter. 您能做的最有效的操作是,创建一个具有onRouteSelected()之类的函数的接口 ,然后在FragmentOne中实现该接口,并将FragmentOne引用传递给您的适配器。 whenever the user clicks on any rout, call the onRouteSelected() of your interface from adapter. 每当用户单击任何路由时,都从适配器调用界面的onRouteSelected()。 This way you will get the RoutNo Clicked callback in Fragment one .Now you can send that data to FragmentTwo and use it as per your need. 这样,您将在片段1中获得RoutNo Clicked回调。现在,您可以将数据发送到FragmentTwo并根据需要使用它。

You have to create interface in first fragment and implement it in MainActivity and in override method of interface call public method of second fragment and update UI in that method

FirstFragment extends Fragment {   

public    onClick mOnClick;
    public interface  onClick
    {
        public void onItenClick(String click);
    }

    @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 {
            mOnClick = (onClick ) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement MyInterface ");
        }
    }

}


public class TabActivity extends AppCompatActivity implements FirstTabFragment.onClick {
 @Override
    public void onItenClick(String click) {

        SecondTabFragment page = (SecondTabFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.vpPager + ":1");
        page.getData(click);
    }
}

and in SecondTabFragment write down your public method
 public void getData(String click)
    {
       //update your UI here
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何将字符串数据从一个片段发送到同一主要活动的另一个片段? - Android: How to send a string data from one fragment to another fragment of the same main activity? 如何通过活动将额外数据从一个片段传输到另一个片段 - how to transfer extra data from one fragment to another through activity 如何在 Android 中将字符串从 Fragment 传输到 Activity - How to transfer String from Fragment to Activity in Android 如何将数据从开始于另一个片段的片段传输到活动 - How to transfer a data from fragment which is started another fragment to an activity 字符串从活动转移到片段 - String transfer from Activity to Fragment 将复选框输入从一个片段传递到同一活动中的另一个片段 - Passing checkbox input from one fragment to another fragment in the same activity 如何将价值从一个片段转移到另一个片段 - How to transfer value from one fragment to another 如何将Arraylist从一个片段传输到另一个片段 - How to transfer Arraylist from one fragment to another 如何在 Android 中从一个片段转移到另一个片段 - How to transfer from one fragment to another in Android 如何将数据从一个 Fragment 传输到另一个 Fragment? - How to transfer data from one Fragment to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM