我有一个关于一个片段的列表视图,它包含所有以特定公共汽车站为特定公交车站的公交车。当用户点击任何路线时我想要。列表视图中列出的,转移到同一活动上另一个片段的自动完成文本视图,并将相同的值复制到autocompletetextview,并从第一个片段的列表中选择。在第二个片段上,用户可以看到该特定公交车的详细行程。他之前选择的是
答案 0 :(得分:1)
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);
}
答案 1 :(得分:0)
您可以做的最有效的方法是使用 onRouteSelected()等功能创建接口,然后在FragmentOne中实现该接口,并将FragmentOne引用传递给您的适配器。每当用户点击任何路径时,从适配器调用接口的onRouteSelected()。这样你就可以在Fragment one中获得RoutNo Clicked回调。现在你可以将这些数据发送到FragmentTwo并根据你的需要使用它。
答案 2 :(得分:0)
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
}