vendredi 8 mai 2015

How can I add a Spinner to RecyclerView

I have followed a tutorial to a navigation drawer to my app, and this works well.

The pattern involves filling the first position of the recyclerview with a header that contains an image and some text views. This is all great, but now I want to add a spinner in there too but am having trouble understanding how to set this up.

The problem is when I create dataAdapter, i get a "cannot resolve constructor". I think it is due to not correctly supplying a Context?

ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);

At this point I am in the ViewHolder class within the Recycler Adapter so "this" isn't an activity. I get that you can use parent.this or activityname.this but this doesn't seem right either (and doesn't work!).

How can I get round the "cannot resolve constructor" error?

Here is the whole onBindViewHolder within my recycler adapter.

@Override
    public void onBindViewHolder(navigationDrawerAdapter.ViewHolder holder, int position) {
        if(holder.HolderID ==1) {                         
            holder.textView.setText(mNavTitles[position - 1]); // Setting the Text with the array of our Titles
            holder.imageView.setImageResource(mIcons[position -1]);// Settimg the image with array of our icons
        }
        else{
            holder.imageProfile.setImageResource(profile);
            holder.textName.setText(name);
            holder.textEmail.setText(email);


            List<String> list = new ArrayList<String>();
            list.add("TEST");

            ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            holder.siteSpinner = (Spinner) findViewById(R.id.siteSpinner);

            holder.siteSpinner.setAdapter(dataAdapter);


        }
    }

Alternatively, is this the right approach? I wonder if creating a "header" in position one is a good way to go? Could an alternative be to create a linear layout for my navigation drawer which has the spinner and recyclerview one after the other?

1 commentaire:

  1. i am facing the same error . if you find the solution then kindly share with me .. you can also contect me on my gmail directly

    RépondreSupprimer