我使用带有两个选项卡的viewpager创建了片段活动,并使用两个textview创建了listview。我想将标签1中的两个文本作为标题和说明发送到标签2。我已经成功发送了描述,但是我没有发送标题(]我只能将一个文本发送到listview。
我的fragment_one.java:
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = (ViewPager)view.findViewById(R.id.viewPager);
Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final ListView list=(ListView)view.findViewById(R.id.list_view);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});
}
interface SendMessage {
void sendData(String message);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}
my fragment_two.java:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_two, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ListView) view.findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(getActivity(), R.layout.single_item,R.id.tvdesc, arrayList);
listView.setAdapter(adapter);
}
protected void displayReceivedData(String message) {
arrayList.add(0,message);
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
您应该使用fragment_one中的回调数据到主要活动,并将数据从活动发送到fragment_two(和刷新视图)
回调片段到您可以使用的活动How to make a callback between Activity and Fragment?
和Fragment_two你可以在Activity中设置这个值, EX, private Fragment mFragmentTwo; ......... mFragmentTwo = new Fragment();
并可以通过功能设置值:mFragmentTwo.displayReceivedData(yourString)