0

Im attempting to add two different views to the GridviewLayoutManager using a custom adapter.

However, I cant seem to reference the headerview correctly. When the onbindViewHolder is called it is expecting a "ViewHolder" response, however i really want to reference the HeaderView i crated

Because I cant access the correct view, I also cant reference the TextView within the XML layout I am calling.

here is my customer adaptor class:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ElementsAdapter extends RecyclerView.Adapter<ElementsAdapter.ViewHolder> {
    private ArrayList<String> mDataset;
    private ArrayList<Integer> mDatamap;

public Context context;

    private static final int VIEW_HEADER = 0;
    private static final int VIEW_NORMAL = 1;

    private View headerView;
    private int datasetSize;

    public class HeaderHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView headertext;

        public HeaderHolder(View v) {
            super(v);
            headertext = (TextView) v.findViewById(R.id.headertext);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView txtHeader;
        public TextView txtFooter;
        public ImageView imgImage;

        public ViewHolder(View v) {
            super(v);
            txtHeader = (TextView) v.findViewById(R.id.firstLine);
            txtFooter = (TextView) v.findViewById(R.id.secondLine);
            imgImage = (ImageView) v.findViewById(R.id.icon);
        }

    }

    public ElementsAdapter(ArrayList<String> myDataset, ArrayList<Integer> myDatamap) {
        mDataset = myDataset;
        myDatamap = mDatamap;
    }


      @Override
    public int getItemViewType(int position) {
        return isHeader(position) == 1 ? VIEW_HEADER : VIEW_NORMAL;
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_HEADER) {
            // create a new view
            View sub_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);

            Context context = sub_view.getContext();

            // set the view's size, margins, paddings and layout parameters
            ViewHolder vh = new ViewHolder(sub_view);

            return vh;


//            return new HeaderViewHolder(headerView);

        } else {
            // create a new view
            View sub_view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sub_layout, parent, false);

            context = sub_view.getContext();

            // set the view's size, margins, paddings and layout parameters
            ViewHolder vh = new ViewHolder(sub_view);

            return vh;


        }
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        if (isHeader(position) == 1) {

            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            final String name = mDataset.get(position);

//        holder.txtHeader.setText(mDataset.get(position));
            viewHolder.headertext.setText(name);

        } else {

    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    final String name = mDataset.get(position);

    Picasso.with(context).load("http://www.500kgiveaway.co.uk/"+name).resize(200,200).into(viewHolder.imgImage);

//        holder.txtHeader.setText(mDataset.get(position));
    viewHolder.txtHeader.setText(name);

    viewHolder.txtHeader.setOnClickListener(new View.OnClickListener()

    {
        @Override
        public void onClick (View v){
        //remove(name);
    }
    }

    );

    viewHolder.txtFooter.setText("Footer: "+mDataset.get(position));
}

        //ViewHolder holder = (ViewHolder) viewHolder;
        //holder.textView.setText("Position " + (position - 1));
    }

    public int isHeader(int position) {
        return mDatamap.get(position) ==1 ? 1:0;}
}
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
PowerMan2015
  • 1,307
  • 5
  • 19
  • 40

1 Answers1

0

It seems to me that the isHeader() method will always return 0, since you compare a String with a integer. I assume you would like want to check the position of the current item to be 1.

Try this code instead:

public boolean isHeader(int position) {
    return position == 1;
}

Then replace

if (isHeader(position) == 1)...

with

if (isHeader(position))...

I hope this helps.

Edit

The above was intended. Sorry. In the class definition ElementsAdapter.ViewHolder is inserted as the ViewHolder type. This works for the normal

ElementsAdapter.ViewHolder extends RecyclerView.ViewHolder

but not for

ElementsAdapter.HeaderHolder extends RecyclerView.ViewHolder

since it doesn't extend ElementsAdapter.ViewHolder.

You should therfore specify RecyclerView.ViewHolder instead as a generic type to support both of your types.

Coen B
  • 725
  • 5
  • 12
  • i wanted this to lookup the value in the MyDataMap array at position (position). If this == 1 then i wanted to return 1, if not then i wanted to return 0 – PowerMan2015 Jun 08 '15 at 17:58
  • Then you will want to convert the String to an int first, to be able to comare them, see http://stackoverflow.com/a/4092190/4983239 . Or you could use a `ArrayList` instead of a `ArrayList`. – Coen B Jun 08 '15 at 18:06
  • sorry my variable naming could be confusing. I have myDataSet which stores the data and myDataMap which denotes the type of view that should be displayed. Do you have any ideas on how i create a second view? at which point i could test the rest of the code – PowerMan2015 Jun 08 '15 at 18:09
  • changed public class ElementsAdapter extends RecyclerView.Adapter to public class ElementsAdapter extends RecyclerView.Adapter but i get an error because the onBindViewHolder requests a ViewHolder. When i change this to RecyclerView.ViewHolder i loose the ability to reference the objects within the view – PowerMan2015 Jun 08 '15 at 18:45
  • here is the error- class "elementsAdapter" must either be declared abstract or implement abstract method onBindViewHolder(VH, int) in 'Adapter' – PowerMan2015 Jun 08 '15 at 18:48
  • What if you cast the ViewHolder to the already expected sub-type? `HeaderView hv = (HeaderView) generic_viewholder` for example. – Coen B Jun 08 '15 at 19:20