我一直试图从listview-onclicklistener获取一个id到三个标签片段。用户首先单击myTicketsFragment,然后转到包含3个可滑动标签的详细信息页面。这些观点已被托管'由一个名为TicketActivity的个人活动。所以目前我已经成功地将数据从片段传递到TicketActivity,但我不能再远远超过它了。一直在寻找2个小时但仍然没有结果..
这是我的代码:
myTicketsFragment :将setOnItemClickListener中的数据传递给标签页活动
public void onItemClick(AdapterView<?> parentView,
View childView, int position, long id) {
Bundle bundle = new Bundle();
bundle.putInt("ticketId", myTickets.get(position).getId());
Intent ticketDetail = new Intent(getActivity(), TicketActivity.class);
ticketDetail.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ticketDetail.putExtras(bundle);
startActivity(ticketDetail);
}
TicketActivity :接收数据并将其传递给3个标签
private ViewPager viewPager;
private TicketTabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Info", "Intern", "Extern" };
public TicketInfoFragment ticketInfoFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
// Receive data
Bundle bundle = getIntent().getExtras();
int ticketId = bundle.getInt("ticketId");
// Pass data to fragments
// ...
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TicketTabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
标签片段的示例
public class TicketInfoFragment extends Fragment {
TicketFull ticket = new TicketFull();
private DatabaseHelper db;
int ticketId;
String androidId;
String authCode;
String platform_url;
int uId;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
db = new DatabaseHelper(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_ticket_info, container, false);
return rootView;
}
}
如果有人能帮助我,我会很高兴
提前致谢
答案 0 :(得分:5)
2个快速方式:
在您的活动中使用静态方法来检索当前的故障单ID
设计并实现一个接口,并将片段注册为活动
第一个选项,在您的活动中:
private static int ticketId;
public static int getCurrentTicketId(){
return ticketId;
}
在您的片段中,您可以执行以下操作:
TickerActivity.getCurrentTicketId();
第二个选项,使用界面:
public interface TicketListener{
public void onTicketChanged(int newTicket);
}
并在您的活动中添加:
public List<TicketListener> listeners = new ArrayList<TicketListener>();
public void addListener(TicketListener listener){
listeners.add(listener);
}
并将每个片段注册为新的侦听器
YourFragment frag = new YourFragment();
addListener(frag);
最后当你想通知监听器的密钥迭代列表时:
for(TicketListener listener : listeners){
listener.onTicketChanged(ticket);
}
答案 1 :(得分:2)
你可以用两种不同的方式做。您的Activity容器中更简单的是必须为要从Fragments访问的数据提供getter,因此在您的片段中必须通过getActivity访问此getter以获取对父的引用并调用get方法,我的意思是:
在TicketActivity中:
private int ticketId;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Receive data
Bundle bundle = getIntent().getExtras();
ticketId = bundle.getInt("ticketId");
...
}
public int getTicketId() {
return ticketId;
}
在你的片段中:
((TicketActivity)getActivity()).getTicketId();
或者更优雅的方式是在初始化片段时通过参数传递Bundle。您必须在TicketTabsPagerAdapter类中执行此操作。我的意思是这样的:
TicketInfoFragment f = new TicketInfoFragment();
f.setArguments(bundle);
要做到这一点,最后一种方法最好使用Singleton模式。您可以点击下一个链接:http://developer.android.com/reference/android/app/Fragment.html
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
答案 2 :(得分:1)
非常简单。何时将捆绑从ActivityA传递到其他ActivityB(包含bundle)。它将由ActivityB类而不是片段接收。
实施起来很简单: 在ActivityB的onResume方法中==&gt;接收捆绑并将附件附加到您所需的片段。检查我的代码从onresume传递给Framgent类
String tag = Constants.TAG_Search;
Fragment fragment;
fragment = fragmentManager.findFragmentByTag(tag);
fragmentTransaction.remove(fragment);
Search searchFragment = new Search();
searchFragment.setArguments(Globals.bd);
fragmentTransaction.add(R.id.tab2, searchFragment, tag);
希望它会有所帮助