How to use setSupportActionBar(toolbar) in Adapter? - android

setSupportActionBar(toolbar);
((Activity)context).setSupportActionBar(toolbar);
I tried many times in different ways but it shows an error.
Anyone, please suggest to me an idea to use it in the adapter.

I got the answer:
((AppCompatActivity) context).setSupportActionBar(toolbar);
before it, I was using it like this. but it didn't work for me.
1.setSupportActionBar(toolbar);
2. ((Activity)context).setSupportActionBar(toolbar);

pass the activity context in your adapter.
Adapter adapter = new Adapter(activity);
Adapter Class::
Context context;
public Adapter(Context c){
this.context = c;
}
Now use context to set ActionBar like:
(Activity) context.getSupportActionBar(toolbar);

Related

How to use Picasso in an Adapter without using context in Fragment

When I try to use context in my fragment I get the error:
constructor Adapter in class Adapter cannot be applied to given types;
required:Context,List<ListItem>,OnItemClickListener
I have declared my context in my adapter as follows:
private Context mContext;
then i initialized the context:
public MyAdapter(Context context,List<ListItem> listItems, OnItemClickListener callback) {
this.listItems = listItems;
this.callback = callback;
this.mContext = context;
}
And used mContext to get my imageurl in the onBindViewHolder using picasso
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
Picasso.with(mContext).load(listItem.getImageurl()).into(holder.imageUrl;
}
But now I can't seem to get around using this context in my Fragment.
This is what I've tried: In my Fragment:
//an error occurs
adapter = new MyAdapter(this,listItems);
so I tried this:
//still get an error
adapter = new MyAdapter(getContext(),this);
I also tried getActivity but still get an error
adapter = new MyAdapter(getActivity());
where am I going wrong?
All I am really trying to do is display the image in my listfragment but I don't know how to use Picasso with using context, and MyAdapter does not require context to function properly. I've been using it without declaring context and the data displays properly. Onclick is also working and displaying strings from firebase, but now I need to display images from Firebase using Picasso into my listfragment. Everything else works fine except this line of code in my Fragment:
adapter = new MyAdapter(getActivity());
You try Get context from any View object in Holder.
Example:
mContext = holder.imageView.getContext()
In your adapter initialization you pass 2 parameters, but your constructor requires 3 parameters.
so try to initialize using 3 params:
adapter=new MyAdapter(getContext(), listItems, this);
getContext()= context of fragment.
lisItems= your list.
this=is your click interface listener (make sure you implemented the listener in your fragment).
Try this, I think you forgot last argument
OnItemClickListener listener = OnItemClickListener {
void onItemClick(int position) {
//some code
}
}
adapter = new MyAdapter(this, listItems, listener);
Fragments are inflated inside an Activity.
In Fragment, you either use the context of the Activity or the context of the whole application.
Plus you have missed passing one more parameter in the Adapter, ie your click listener.
Define the adapter like this -
OnItemClickListener mOnItemClickListener = OnItemClickListener {
void onItemClick(int position) {
}
}
adapter = new MyAdapter(getActivity(), listItems, mOnItemClickListener);
or
adapter = new MyAdapter(getActivity().getApplicationContext(), listItems, mOnItemClickListener);

Error in initialization of custom array adapter

I am trying to make a custom listview. The list is declared as below
List<DocRow> doctors = new ArrayList<>();
This list is then being populated.
My custom array adapter is in a separate class with its constructor declared as below.
public class DocAdapter extends ArrayAdapter<DocRow>{
Context context;
int resource;
ArrayList<DocRow> doctors;
private LayoutInflater inflater;
public DocAdapter(#NonNull Context context, #LayoutRes int resource, ArrayList<DocRow> doctors) {
super(context, resource, doctors);
this.context = context;
this.resource = resource;
this.doctors = doctors;
inflater = LayoutInflater.from(context);
}
Now in my main activity, I am trying to create a new custom array adapter by passing off my list (which is a valid parameter), it isn't accepted. The code for creation and setting of adapter for linking the listview with the list is below.
DocAdapter adapter = new DocAdapter(getApplicationContext(), R.layout.doc_row, doctors);
docList.setAdapter(adapter);
Can anyone explain what is the issue? The link for error screenshot is above. I tried searching for this specific issue, but haven't been able to find a solution that works.
Change your constructor argument to List instead of ArrayList as you are passing list in it.
List<DocRow> doctors;
public DocAdapter(#NonNull Context context, #LayoutRes int resource, List<DocRow> doctors) {
super(context, resource, doctors);
this.context = context;
this.resource = resource;
this.doctors = doctors;
inflater = LayoutInflater.from(context);
}
As pointed by #Tim, here is a little detail about why this is needed.
When an instance is initialized, it may be initialized with one of its child classes but the object remains an instance of Super class only(Due to runtime polymorphism) and therefore the methods that consume this instance either expect super class or the instance should be casted to superclass before passing it on.
The easiest way to identify is to always look at the type on the left-hand side instead.
List a=new ArrayList();
In above example, the instance is actually an arraylist but it is of Type List.
A parent class's reference can store subclass's object, but the reverse is not true.
Here, in the constructor of your adapter, you have ArrayList<DocRow> as your parameter type, but your doctors list is of type List<DocRow>. You, you're passing a List<> object to an ArrayList<> reference.
To solve it, either change your doctors variable type to ArrayList<>, or your constructor parameter type to List<>

Start activity from a AdapterRecyclerView

I have a viewPager with some fragments (in a fragment I have a recyclerView ), recyclerView contains more items, so, I want to when I click a item on recyclerView,it trans to BBBBActivity. But I don't figure out.
it errors
thank you very much
try this
Intent intent= new Intent(itemView.getContext(),addsongList.class);
itemView.getContext().startActivity(intent);
change your code like this or pass the Context in your adapter
Intent intent= new Intent(itemView.getContext(),AddSongToList.class);
getActivity.startActivity(intent);
You need a context to call startActivity(). Pass getActivity() as a context from your Fragment where you are initializing your adapter; like:
YourAdapter adapter = new YourAdapter(getActivity, /* other parameters*/);
In adapter:
private Context mContext;
YourAdapter(Context context, /*other parameters*/) {
mContext = context;
}
And then use mContext to call startActivity():
mContext.startActivity();

getting context of fragment in baseadapter

this has became some thing complicated for me since im not so much familiar with fragments but it might also be simple for some of you guys, here i had this part of code referring to an activity, when i changed the activity to fragment it says can not cast from context to ListViewActivity, can you please help me solve this:
#Override
public Filter getFilter() {
return ((ListViewActivity)mContext).new ListFilter();
}
obviously mContext is a context reference.i understand that inside the fragment should get context with getActivity(), but from outside ?thanks a lot.
I would construct a custom adapter similar like this:
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Fragment fragment, List<RowItem> items) {
this.context = fragment.getActivity();
this.rowItems = items;
}
}
And in your fragment, call the adapter like this:
CustomBaseAdapter adapter = new CustomBaseAdapter(this, items);
Now you can cast the context in your adapter to ListViewActivity, assuming the fragment is part of ListViewActivity.
Hope this helps!

Access to getString() in android.support.v4.app.FragmentPagerAdapter?

In a class extending android.support.v4.app.FragmentPagerAdapter, is there any way to get access to the Context.getString(..) method without the extending class being an inner class of an activity or passing in some context from the outside?
Thanks for any hint!
From a fragment use :
getActivity().getString(...)
From an adapter use :
getContext().getResources().getString(...)
Yes, you need a context to access the resources.
From an Activity, use:
this.getString(R.string.string_name);
From a Fragment, use:
getActivity.getString(R.string.string_name);
From an adapter, use:
getContext().getResources().getString(R.string.string_name);
I had a similar issue. From a drawer layout, I wanted to decide which fragment to use in a method called from a helper class.
So in onCreateView...
String form = getResources().getStringArray(R.array.drawer_array)[i];
Context context = getActivity().getApplicationContext();
FragmentHelper fh = new FragmentHelper();
int myFragment = fh.getCurrentFragment(form,context);
And in public FragmentHelper()...
public int getCurrentFragment(String form, Context context){
...
context.getResources().getString(R.string.label_item1);
...
}
The trick being to add context in front of getResources(). Otherwise, my stack showed that the fragment was not attached to an activity.
Hope this helps someone.

Categories

Resources