Android将对象列表从活动传递到选项卡碎片

时间:2017-01-03 17:18:04

标签: android android-fragments

我在这里遇到了问题 我有tablayout viewpager的活动从数据库中获取对象并通过bundle将它们传递给tabs片段 问题是选项卡片段

中的bundle变为null

这是活动

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;


public class Categorys extends DrawerActivity implements TabLayout.OnTabSelectedListener {

    String url,result;

    //This is our tablayout
    private TabLayout tabLayout;
    //This is our viewPager
    private ViewPager viewPager;
    ProgressDialog progress;
    DBhelper db;
    Bundle bundle;

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categorys);
        //Adding toolbar to the activity
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //       getSupportActionBar().setDisplayShowHomeEnabled(true);
        set();

        db = new DBhelper(Categorys.this);
        db.open();
        List<ProductObj> productslistfromDB = db.getAllProductsUp();
        db.close();

    ProductObj product = productslistfromDB.get(0);
    String namee = product.getName();
    String details = product.getDetails();
    String category = product.getcategory();
    String ownerr = product.getOwner();
    String weight = product.getWeight();
    String size = product.getSize();
    String price = product.getPrice();
    String imageurl = product.getimageurl();
    String owner_address = product.getOwner_address();
    String owner_phone = product.getOwner_phone();
    String UID = product.getUID();

    productpojo productpoj = new productpojo();
    productpoj.setName(namee);
    productpoj.setDetails(details);
    productpoj.setcategory(category);
    productpoj.setOwner(ownerr);
    productpoj.setWeight(weight);
    productpoj.setSize(size);
    productpoj.setPrice(price);
    productpoj.setimageurl(imageurl);
    productpoj.setOwner_address(owner_address);
    productpoj.setOwner_phone(owner_phone);
    productpoj.setUID(UID);

    ArrayList<productpojo> productlisttosend = new ArrayList<>();
    productlisttosend.add(productpoj);

    Tab1 fragmentGet1 = new Tab1();
    Tab2 fragmentGet2 = new Tab2();
    bundle = new Bundle();
    bundle.putParcelableArrayList("arraylist", productlisttosend);
    fragmentGet1.setArguments(bundle);
    fragmentGet2.setArguments(bundle);

    //Initializing the tablayout
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    //Adding the tabs using addTab() method
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_clothes));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_electrical));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_hammer));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_cars));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_interior_accesories));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_buid));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);

    //Creating our pager adapter
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    //Adding adapter to pager
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    //Adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(this);

    }




@Override
public void onTabSelected(TabLayout.Tab tab)
{
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
@Override
public void onBackPressed() {
}
}

这是第一个片段Tab1

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;

/**
 * Created by toshiba c660-12T on 16/08/2016.
 */
public class Tab1 extends Fragment {

    GridView gv;
    int[] clicknums;
    ArrayList<productpojo> productarraylist = null;
    Bundle bundle;


    //Overriden method onCreateView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab1, container, false);

         bundle=this.getArguments();
        if (bundle != null) {
            productarraylist = bundle.getParcelableArrayList("arraylist");
        }
        go();

        return view;
    }



    public void go(){
    productpojo propoj; = productarraylist.get(0);
    String namee = propoj.getName();
    String details = propoj.getDetails();
    String category = propoj.getcategory();
    String ownerr = propoj.getOwner();
    String weight = propoj.getWeight();
    String size = propoj.getSize();
    String price = propoj.getPrice();
    String imageurl = propoj.getimageurl();
    String owner_address = propoj.getOwner_address();
    String owner_phone = propoj.getOwner_phone();
    String UID = propoj.getUID();

    ProductObj product = new ProductObj();
    product.setName(namee);
    product.setDetails(details);
    product.setcategory(category);
    product.setOwner(ownerr);
    product.setWeight(weight);
    product.setSize(size);
    product.setPrice(price);
    product.setimageurl(imageurl);
    product.setOwner_address(owner_address);
    product.setOwner_phone(owner_phone);
    product.setUID(UID);

    final ArrayList<ProductObj> productlist = new ArrayList<>();
    productlist.add(product);

    gv = (GridView) getView().findViewById(R.id.grid);
    clicknums = new int[productlist.size()];
    gv.setAdapter(new CustomGrid(getActivity(), productlist));
    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {

    int clicknum;
    clicknums[position] = clicknums[position] + 1;
    clicknum = clicknums[position];
    String image = productlist.get(position).imageurl;
    String text = productlist.get(position).name;
    String owenr = productlist.get(position).owner;
    Intent intent = new Intent(getActivity(), Product.class);
    intent.putExtra("numofclick", clicknum);
    intent.putExtra("title", text);
    intent.putExtra("owner", owenr);
    intent.putExtra("image", image);

    //Start details activity
    startActivity(intent);
    }
    });
    }

这是第二个片段Tab2

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;

/**
 * Created by toshiba c660-12T on 16/08/2016.
 */
public class Tab2 extends Fragment {

    GridView gv;
    int[] clicknums;
    ArrayList<productpojo> productarraylist = null;
    Bundle bundle;


    //Overriden method onCreateView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab2, container, false);

        bundle=this.getArguments();
        if (bundle != null) {
            productarraylist = bundle.getParcelableArrayList("arraylist");
        }
        go();

        return view;
    }



    public void go(){
    productpojo propoj; = productarraylist.get(0);
    String namee = propoj.getName();
    String details = propoj.getDetails();
    String category = propoj.getcategory();
    String ownerr = propoj.getOwner();
    String weight = propoj.getWeight();
    String size = propoj.getSize();
    String price = propoj.getPrice();
    String imageurl = propoj.getimageurl();
    String owner_address = propoj.getOwner_address();
    String owner_phone = propoj.getOwner_phone();
    String UID = propoj.getUID();

    ProductObj product = new ProductObj();
    product.setName(namee);
    product.setDetails(details);
    product.setcategory(category);
    product.setOwner(ownerr);
    product.setWeight(weight);
    product.setSize(size);
    product.setPrice(price);
    product.setimageurl(imageurl);
    product.setOwner_address(owner_address);
    product.setOwner_phone(owner_phone);
    product.setUID(UID);

    final ArrayList<ProductObj> productlist = new ArrayList<>();
    productlist.add(product);

    gv = (GridView) getView().findViewById(R.id.grid);
    clicknums = new int[productlist.size()];
    gv.setAdapter(new CustomGrid(getActivity(), productlist));
    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {

    int clicknum;
    clicknums[position] = clicknums[position] + 1;
    clicknum = clicknums[position];
    String image = productlist.get(position).imageurl;
    String text = productlist.get(position).name;
    String owenr = productlist.get(position).owner;
    Intent intent = new Intent(getActivity(), Product.class);
    intent.putExtra("numofclick", clicknum);
    intent.putExtra("title", text);
    intent.putExtra("owner", owenr);
    intent.putExtra("image", image);

    //Start details activity
    startActivity(intent);
    }
    });
    }

寻呼机课程

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
 * Created by toshiba c660-12T on 16/08/2016.
 */

//Extending FragmentStatePagerAdapter
public class Pager extends FragmentStatePagerAdapter {

    //integer to count number of tabs
    int tabCount;

    //Constructor to the class
    public Pager(FragmentManager fm, int tabCount) {
        super(fm);
        //Initializing tab count
        this.tabCount= tabCount;
    }

    //Overriding method getItem
    @Override
    public Fragment getItem(int position) {
        //Returning the current tabs
        switch (position) {
            case 0:
                Tab1 tab1 = new Tab1();
                return tab1;
            case 1:
                Tab2 tab2 = new Tab2();
                return tab2;
            default:
                return null;
        }
    }

    //Overriden method getCount to get the number of tabs
    @Override
    public int getCount() {
        return tabCount;
    }
}

1 个答案:

答案 0 :(得分:2)

需要致电getArguments()中的onCreateView以获取使用setArguments发送的Bundle:

bundle=this.getArguments();