getting context of fragment in baseadapter - android

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!

Related

How to use setSupportActionBar(toolbar) in Adapter?

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);

How to access variable in a Activity, from childView?

I have a view as following hierarchy.
The main Activity has
ArraylistArrayListArraylist()
I want to access and change the data of the Arraylist from the button of the Card view. I'm using Custom ArrayAdapter. So is there are any way to do this?
You just need to pass your list data from activity to your custom adapter by calling adapter's constructor. See below how you can pass list data and that from adapter.
public TestAdapter extends ArrayAdapter<String>{
private Context mContext;
private List<String> list = new ArrayList<>();
public MovieAdapter(#NonNull Context context, #LayoutRes ArrayList<String> list) {
super(context, 0 , list);
this.mContext = context;
this.list = list;
}
.............//use "list" in your adapter
}
In activity you have below list.
Arraylist a = new Arraylist();
a.add("test");
a.add("test1");
a.add("test2");
a.add("test3");
TestAdapter testadapter=new TestAdapter(this,a);
Now you have that list in adapter and you can use list in your adapter.

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);

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();

Listview and array adapter and remove

I created an adapter
public class MyArrayAdapter extends ArrayAdapter<Watch>
overriding
public MyArrayAdapter(Context context, List<Watch> values){
super(context, android.R.layout.simple_list_item_1, values);
this.context = context;
}
and
private View createOneView(){
...
}
Then i do
MyArrayAdapter myAdapter = new MyArrayAdapter(this, getAllWatches());
... //DO some stuff here
myAdapter.remove(getAllWatches().get(2));
myAdapter.notifyDataSetChanged();
The watch is still not removed. I suspect that objects are removed only if ther are == and not .equal(), or am I just missing something more trivial?
myAdapter.getPosition(getAllWatches().get(2)) return -1, so the problem is the object is not found. ArrayAdapater uses indeed .equals() method. Implementing it in the appropriate way (comparingfields) everything works smoothly.

Categories

Resources