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.
Related
I have a graph in main activity also I have a recycler view in main activity. Custom adapter is used for recyclerview. I have a check box and swipe layout in list item layout. in swipe layout there is a delete button.
I want to reset the graph of main activity when I check the check box or when I delete any item.
For this I created one method in main activity. And called this method in adapter onCheckedChangeListener and on click of delete.
But I am getting a null pointer exception on mBarChart. i.e . graph. I have instantiated in mBarChart in setUI method and this is called in onCreate of an activity.
resetMethod
public void resetGraph(Context context)
{
mBarChart.invalidate();
}
in adapter :
Context conext;
MainActivity mainActivity;
mainActivity = new MainActivity();
mainActivity.resetGraph(conext);
How to do this? Please help.. Thank you..
In Adapter call your resetMethod this way
((MainActivity)context).resetGraph(context);
Create a interface that implement Activity, Main activity in your case and override method and perform operation.
//Interface
public interface OnRefreshViewListner{
public void refreshView();
}
//Main Activity
MainActivity extends Activity implements OnRefreshViewListner
{
//Other methods
#Override
public void refreshView(){
// write refresh code here
}
}
//Initialize Interface in adapter constructor
public class YourAdapter extends BaseAdapter {
private OnRefreshViewListner mRefreshListner;
public YourAdapter (Context context) {
mRefreshListner = (OnRefreshViewListner)context;
}
//call MainActivity method
mRefreshListner.refreshView();
}
In the adapter, you should not create a new instance of MainActivity and call resetGraph(). You should use the instance of MainActivity, that created the adapter. Send the instance of MainActivity to the adapter, new Adapter(this) and save it in adapter.
You can change a view from the context of an adapter like this :
cast context to activity.
use findviewbyid method to find the view you want.
initiliaze it to a variable.
View v = ((Activity)getContext()).findViewById(WHATEVER_VIEW_COMPONENT_YOU_WANT);
change the variable as you want.
note. Don't forget to use the type of view that you want and cast the findview method to it.
If you want to call a method just cast the context to MainActivity and call it.
I use MvxRecyclerView in Mvvmcross Support RecyclerView library
The problem is that MvxRecyclerView cannot be inflated in an Activity (but Fragment works well). It seems that MvxAndroidBindingContextHelpers.Current() return null in MvxRecyclerAdapter constructor.
Could you please tell me how to resolve this problem ?
Maybe OnCreate is too early in the lifecycle to get a context. You could try to do this in the OnCreateView method. Otherwise i would suggest to set your own instance of the MvxRecyclerAdapter where you put in the context in the constructor.
var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
if (recyclerView != null)
{
recyclerView.Adapter = new MvxRecyclerAdapter((IMvxAndroidBindingContext)BindingContext);
}
I'm new to android and i'm to instantiate a Drawable reference.
If the class extends Activity we could directly use this..
Drawable myDrawable = getResources().getDrawable(R.drawable.image1);
iv.setImageDrawable(myDrawable);
As my class is an custom Adapter the getResources method is not available.
Is there any way around...?
I have already tried this...
imageview.setTag(R.drawable.stack_default);
Any help appreciated :)
When the Adapter is constructed, pass the activity context to the adapter and save it as a member variable.
Or
If you have the Application Instance then get reference to that and then extract the drawable.
ImageView has the setImageResources(int resId) method which takes as parameter the id of the resouces you need, si, in this case there is no need to use getResources(). Anyway you could use the context of the ViewGroup parent paramter of getView(..)
You can retrieve it this way:
Context context = parent.getContext();
then you can use it to retrive the Resources
context.getResources()
You want to check if (parent != null)
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(...)
I am using WebView in an ActivityGroup and it will throw an exception if WebView show Dialog and complain the activity is not valid. But it's okay if I set the context of the WebView to the TOP activity. So I wish to know how to set the context in the layout xml ?
You can use layoutinflater to achieve this:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.yourLayoutName, null);
this.theSpinner = (Spinner) viewToLoad.findViewById(R.id.Spinny);
this.setContentView(viewToLoad );
Hope that helps. for dialog you can just change context from this to
this.getParent()
So I wish to know how to set the context in the layout xml ?
That's not possible. Though, I have some experience with ActivityGroup, so I know how to solve this problem:
// in your ActivityGroup...
public class YourActivityGroup extends ActivityGroup{
public static YourActivityGroup self;
public YourActivityGroup(){
self = this;
// the rest of your code here
}
}
Then, when you need a context in order to show a dialog or a Toast or whatever, you use YourActivityGroup.self.