如何将数据传递给标签页

时间:2015-12-25 04:44:09

标签: java android

我需要将Recyclerview中的数据传递给标签页。而且我不知道如何。我尝试使用intent但我的问题没有解决。帮帮我们。

Recycler_View_Adapter.java

public class Recycler_View_Adapter extends RecyclerView.Adapter<Recycler_View_Adapter.View_Holder> {

    //    List<Data> dataSet = Collections.emptyList();
    List<Data> mData;
    Context context;
    Intent intent;

    @Override
    public void onBindViewHolder(View_Holder holder, int position) {
        //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
        final Data itemData = mData.get(position);
        holder.title.setText(mData.get(position).title);
        holder.description.setText(mData.get(position).description);
        holder.imageView.setImageResource(mData.get(position).imageId);

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            intent = new Intent(context, Tab.class);
            Bundle bundle = new Bundle();
            bundle.putString("key", itemData.getTitle());
            TabOne fragmentOne = new TabOne();
            fragmentOne.setArguments(bundle);
            context.startActivity(intent);
            }
        });
    }
}

Tab.java

public class Tab extends AppCompatActivity {
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    Intent intent;
    public String id;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);

        Bundle b;
        b = getIntent().getExtras();
        String id = b.getString("IdValue");
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new TabOne(), "About");
        adapter.addFragment(new TabTwo(), "Places");
        adapter.addFragment(new TabThree(), "Hotels");
        adapter.addFragment(new TabFour(), "Restaurants");
        adapter.addFragment(new TabFive(), "Recreational");
        viewPager.setAdapter(adapter);
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

}

TabOne.java

public class TabOne extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_one, container, false);
        TextView label = (TextView) view.findViewById(R.id.textViewOne);
        String strtext = getArguments().getString("key");
        label.setText(strtext);
        return view;
    }
}

TabTwo.java

public class TabTwo extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_two, container, false);
    }
}

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
}

我需要将来自recyclerView的数据传递给选项卡。我尝试了意图,但它没有回答我的问题。

我收到此错误

12-25 01:24:16.040 2569-2569/com.example.android.buktogo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.android.buktogo, PID: 2569
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                               at com.example.android.buktogo.Tabs.TabOne.onCreate(TabOne.java:22)
                                                                               at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
                                                                               at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:926)
                                                                               at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:879)
                                                                               at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:152)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:1104)
                                                                               at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                               at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
                                                                               at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                               at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                               at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                               at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                               at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
                                                                               at android.view.View.measure(View.java:17547)
                                                                               at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
                                                                               at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:135)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案