When adding a navigation drawer to my app, I used a pattern that involves filling the first position of the RecyclerView
with a header that contains an image and some TextViews
. 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 RecyclerAdapter
so "this" isn't an Activity
. I get that you can use parent.this or activity name.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 RecyclerAdapter
.
@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 LinearLayout
for my navigation drawer which has the Spinner
and RecyclerView
one after the other?
I would suggest creating the dataAdapter object in the method you use to initialize the RecyclerAdapter (ie: constructor) and pass to that method the context of the caller.
ex:
ArrayAdapter<String> dataAdapter;
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
public MyRecyclerAdapter(Vector<int> myItems, Context leContext) {
...
dataAdapter =new ArrayAdapter<String>(leContext, android.R.layout.simple_spinner_item,list);
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.