package com.codemine.unofficial.pcpartpicker;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

 * Computer Part Adapter Class with ComputerPartViewHolder inner class.
 * This class is an adapter for populating the recyclerview of each computer part class.
 * The ComputerPartViewHolder is described above it.

public class ComputerPartAdapter extends RecyclerView.Adapter<ComputerPartAdapter.ComputerPartViewHolder> {
    public ArrayList<String[]> stringArrayList = new ArrayList<>();
    public ArrayList<String> urls = new ArrayList<>();
    public Context context;
    public static int listSize;

    public ComputerPartAdapter(String[][] arr, ArrayList<String> urls, Context context) {//add url
        this.stringArrayList = new ArrayList<>(Arrays.asList(arr));
        this.urls = urls;
        this.context = context;

        listSize = arr[0].length;

    public ComputerPartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);
        return new ComputerPartViewHolder(view);

    public void onBindViewHolder(ComputerPartViewHolder holder, int position) {
        String[] curr = stringArrayList.get(position);

        holder.url = "" + urls.get(position);//setup ViewHolder
        //TODO set image dynamically
        //set data from String[] to views
        for (int i = 0; i < curr.length; i++) {

        try {
            //TODO add null checks (shouldnt be necessary but for a failsafe)
            final Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(holder.url));
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
        } catch (NullPointerException e){
            Log.i("Uri FAILED", curr[0]);

    public int getItemCount() {
        return stringArrayList.size();

     * Holds the views for each computerpart that is displayed as a card to the user
     * The amount of textview in each Holder is decided by the static variable
     * listSize in the enclosing class. Right now this seems to be the best way
     * to programatically set the amount of textviews in each holder.
    public static class ComputerPartViewHolder extends RecyclerView.ViewHolder {

        ImageView computer_part_img;
        TextView[] textViews;
        CardView cardView;
        String url;

        public ComputerPartViewHolder(View view) {
            cardView = (CardView)view.findViewById(;
            //TODO change the image programatically
            computer_part_img = (ImageView)view.findViewById(;

            LinearLayout linearLayout = (LinearLayout) view.findViewById(;
            textViews = new TextView[ComputerPartAdapter.listSize + 1]; //for space
            for (int i = 0; i < textViews.length; i++) {
                textViews[i] = new TextView(view.getContext());