Start activity from a AdapterRecyclerView - android

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

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

How to implement custom adapters properly?

I am trying to wrap my head around implementing adapters properly and would like to know the ff:
Is it okay to pass an activity context to an adapter? I've read somewhere it is bad practice to pass around activity context and find it confusing.
In getting the parent view e.g to be used in SnackBar etc., is instantiating it via constructor like this
this.mRootView =
((Activity)mContext).getWindow().getDecorView().findViewById(R.id.activity_country_search);`
the right way or should it be instantiated in onCreateViewHolder and utilize the View parent parameter like this:
```
#Override
public CountryIssuingViewHolder onCreateViewHolder(ViewGroup parent) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_country_search_issuing_row, parent, false);
this.mRootView = parent.findViewById(R.id.activity_country_search);
return new CountryIssuingViewHolder(v, true);
}
Is it okay to implement snackbar in the adapter or should I use eventbus and show it via activity?
Current adapter implementation:
Activity
adapter = new CountryIssuingListAdapter(this, countryIssuingList, new
CountryPhoneCodeAdapter.OnItemClickListener() {
#Override
public void onItemClick(Country item, View v) {
Gson gson = new Gson();
Intent intent = new Intent();
intent.putExtra("COUNTRY", gson.toJson(item));
setResult(RESULT_OK, intent);
finish();
}
});
Adapter
public CountryIssuingListAdapter(Context mContext, ArrayList<Country> itemArrayList, CountryPhoneCodeAdapter.OnItemClickListener listener) {
this.countryArrayList = itemArrayList;
this.countryArrayListForSearch = new ArrayList<>();
this.countryArrayListForSearch.addAll(itemArrayList);
this.mContext = mContext;
this.TAG = mContext.getClass().getSimpleName();
this.mRootView = ((Activity)mContext).getWindow().getDecorView().findViewById(R.id.activity_country_search);
this.listener = listener;
}
Thanks a lot.
I don't see anything bad here. But in most cases you don't need a Context inside an adapter
2-3. Do you use a SnackBar to diplay an event (e.g. click, swipe)? If so, then you should pass a listener to the adapter and implement it in an activity like you did for OnItemClick. This way the adapter's work is only to display items and notify about User's actions but do not handle these actions
P.S. I would argue about using an EventBus instead of listeners but that's not the topic

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!

Starting another activity without extending Activity superclass

I have a View class with some clickable bitmaps,in the onTouch method,i want to trigger a new activity when i have touched the bitmaps
Intent newintent = new Intent();
newintent.setClass(view.getContext(),MainMenu.class);
startActivity(newintent);
Since the class does not extends Activity,how can i start an activity without extending Activity?
the current error is :
The method startActivity(Intent) is undefined for the type DrawView
Provided that your MainMenu.class is an Activity and you call the startActivity() method from a View of some sort you need to add a Context from which your new Activity will be started.
In your case it would be:
view.getContext().startActivity(newintent);
You need to have a context to do that something like this should be okay.
Declare a context for your view at the head of your class.
Context myContext = view.getContext();
And then use it to start your activity.
Intent newintent = new Intent();
newintent.setClass(myContext,MainMenu.class);
myContext.startActivity(newintent);myContext.startActivity(newintent);
When you are initializing your View class. In the constructor you pass Context of your activity class e.g
View v = new View(context)
In you Own View class constructor. Make a reference of this context as class-level object.
public class MyView extend View{
private Context mContext = null;
MyView(Context context){
super(context);
mContext = context;
}
}
and when need to start a activity.
mContext. startActivity(newintent);
You can deliver the activity which the view in the show to this view.
The constructor of the view could be rewritten as:
View(Contenxt , ..)
And will use
context.start(...)

Categories

Resources