Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch Collections or Categories list from shopify ?

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 672
    Comment on it

    Shopify sdk provides interface to get the saved categories of store on shopify and you can get that in your android app. you can show lots of information in list view like image of product, name, date, price etc.

    1. Activity class to show categories list :

    public class CollectoinsListActivity extends AppCompatActivity implements ShopifyOperationInterface {
    
        private ArrayList<Collection> mArrayList;
        private RecyclerView rvCollections;
        private RecyclerView.Adapter mAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_collectoins_list);
    
            TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
            rvCollections = (RecyclerView)findViewById(R.id.rvCollections);
            rvCollections.setHasFixedSize(true);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(CollectoinsListActivity.this);
            rvCollections.setLayoutManager(mLayoutManager);
            rvCollections.setItemAnimator(new DefaultItemAnimator());
            tvTitle.setText(AppConstant.sBuyWine);
    
            mArrayList = ShopifyOperationsHandler.getCategoriesList(CollectoinsListActivity.this);
        }
    
        @Override
        public void success(String aType) {
    
            if (aType.equalsIgnoreCase(CollectoinsListActivity.class.getName())) {
                Log.e("list size:", mArrayList.size() + "");
                if (mArrayList != null && mArrayList.size() > 0) {
                    mAdapter = new AdapterCollectionsList(mArrayList, CollectoinsListActivity.this);
                    rvCollections.setAdapter(mAdapter);
                }
            }
        }
    
    }
    

    2. Adapter class to adapt items in list :

    public class AdapterCollectionsList extends RecyclerView.Adapter<AdapterCollectionsList.MyViewHolder> {
    
        private ArrayList<Collection> mCollectionList;
        private Activity mActivity;
    
        public AdapterCollectionsList(ArrayList<Collection> aCollectionList,Activity aActivity){
            mCollectionList = aCollectionList;
            mActivity = aActivity;
        }
    
        public static class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tvCollectionName;
            ImageView imgCollection;
            ProgressBar progressbarCollections;
    
            public MyViewHolder(View itemView) {
                super(itemView);
                this.tvCollectionName = (TextView) itemView.findViewById(R.id.tvCollectionName);
                this.imgCollection = (ImageView) itemView.findViewById(R.id.imgCollection);
                this.progressbarCollections = (ProgressBar) itemView.findViewById(R.id.progressbarCollections);
            }
        }
    
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_collections, parent, false);
    
            MyViewHolder myViewHolder = new MyViewHolder(view);
    
            return myViewHolder;
        }
    
        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
    
            TextView tvCollectionName = holder.tvCollectionName;
            ImageView imgCollection = holder.imgCollection;
            final ProgressBar progressBar = holder.progressbarCollections;
            tvCollectionName.setText(mCollectionList.get(listPosition).getTitle());
        }
    
        @Override
        public int getItemCount() {
            return mCollectionList.size();
        }
    }

    3. Create recycler view accordingly in main activity layout and this is item layout file :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_marginTop="@dimen/padding_ten"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:layout_width="match_parent"
            android:id="@+id/imgCollection"
            android:scaleType="fitXY"
            android:background="@drawable/lastcall"
            android:layout_height="@dimen/collection_img_height" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:background="@color/colorTransparentLight"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="horizontal"
            android:layout_height="@dimen/collection_img_height">
    
            <TextView
                android:layout_width="match_parent"
                android:id="@+id/tvCollectionName"
                android:textSize="@dimen/text_size_large"
                android:text="sample"
                android:gravity="center"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:layout_height="@dimen/collection_img_height"/>
    
            <ImageView
                android:layout_width="wrap_content"
                android:id="@+id/imgNext"
                android:scaleType="fitXY"
                android:layout_marginRight="@dimen/padding_ten"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:background="@drawable/arrow"
                android:layout_height="wrap_content" />
    
            <ProgressBar
                android:id="@+id/progressbarCollections"
                android:layout_width="wrap_content"
                android:layout_centerInParent="true"
                android:background ="@drawable/progress"
                android:layout_height="wrap_content" />
    
        </RelativeLayout>
    </RelativeLayout>

    4. Method to fetch categories list :

    public static ProgressDialog mProgressDialog;
    
    public static ArrayList<Collection> getCategoriesList(final Activity aActivity) {
    
        final ArrayList<Collection> mArrayList = new ArrayList<>();
        final ShopifyOperationInterface mShopifyOperationInterface = (ShopifyOperationInterface)aActivity;
    
        initializeProgress(aActivity);
    
        showProgress(aActivity);
    
        SampleApplication.getBuyClient().getCollections(1, new Callback<List<Collection>>() {
    
            @Override
            public void success(List<Collection> collections) {
                Log.e("collections list size:", collections.size() + "");
    
                if (collections.size() > 0) {
                    for (int i = 0; i < collections.size(); i++) {
                        Log.e("collections id:", collections.get(i).getCollectionId() + "");
                        Log.e("collections id:", collections.get(i).getTitle() + "");
                        Log.e("collections id:", collections.get(i).getHtmlDescription() + "");
                        Log.e("collections id:", collections.get(i).getImageUrl() + "");
                        Log.e("collections id:", collections.get(i).getPublishedAtDate() + "");
    
                        mArrayList.add(collections.get(i));
                    }
                }
                mShopifyOperationInterface.success(CollectoinsListActivity.class.getName());
            }
    
            @Override
            public void failure(BuyClientError error) {
                Log.e("collections error:", error.getMessage());
                dismissProgress(aActivity);
            }
        });
        dismissProgress(aActivity);
    
        return mArrayList;
    
    }
    

    In our next tutorial we will fetch product of particular category.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: