具有不同内容的多个标签中的AsyncTask

时间:2015-11-21 12:28:50

标签: android android-fragments android-asynctask android-recyclerview

我的应用程序有10个选项卡(分类)每个选项卡都需要从远程服务器上的PHP / MySQL WebService调用不同的数据集。

我将类别id(catid)传递给参数中的doinBackground函数然后我检索json并构建数组iList:

@Override
protected List doInBackground(String... params) {

     int catid = Integer.parseInt(params[0]);

    ProductApi  prd = new ProductApi();

    try {

        List<ProductDetails> arrayproduct = prd.ProductApi(catid);

        ArrayList<Data> al = new ArrayList<Data>();

        for (ProductDetails prditem : arrayproduct) {

            al.add(new Data(new String[] { 
                    "Product", 
                    prditem.getProductName(),
                    prditem.getProductPrice()},
                    new int[] { R.drawable.popularity_img5 }));
            }

        iList.addAll(al);   

    return arrayproduct;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

我面临的问题 是因为我在所有选项卡中都有相同的产品列表,因为我不知道在每个水平滚动条上执行AsyncTask的位置,检索当前的CatId(类别ID)并在当前选项卡上加载用户的新数据正在观看。 我必须创建许多适配器作为我有的类别标签吗?

我可以在instantiateItem上获取CatId(类别ID)但是它不允许我执行loaddata方法getItemCount()在这种情况下返回数组列表0

@Override
        public Object instantiateItem(ViewGroup container, int pos) {


            int CatId = tinydb.getInt("CategoryId"+pos);

            Log.w("PageAdapter", "-------------------------------------instantiateItem pos:"+pos+" CatId: "+CatId);


            final View v = getLayoutInflater(null).inflate(R.layout.pager_card_view, null);

            RecyclerView recList = (RecyclerView) v.findViewById(R.id.cardList);
            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            recList.setHasFixedSize(true);
             //LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            StaggeredGridLayoutManager llm = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
            llm.setOrientation(StaggeredGridLayoutManager.VERTICAL);
             // use a linear layout manager
            recList.setLayoutManager(llm);

            // create an Object for Adapter
            CardAdapter ca = new CardAdapter();
            // set the adapter object to the Recyclerview
            recList.setAdapter(ca);



            ((MainActivity) getActivity()).enableActionBarAutoHide(recList);

            container.addView(v,
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT);




            return v;
        }

0 个答案:

没有答案