How to get a Drawable instance in class that extends BaseAdapter - android

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)

Related

Why do we need to pass a resource layout to a custom adapter in constructor?

I am curious to know this as I am not able to find it in internet.
I am creating a custom list adapter. In this adapter there is a getView method which I am overriding. In this method I inflate the row layout which I pick from layout folder.
In this case I should not need to pass the same row layout as a constructor to the adapter, which is not allowed.
Please answer.
Regards
Utsav.
you can safely ignore it by overriding the constructor, like this:
public class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context){
super(context, 0);
}
}
edit:
That exists for cases that you're not creating a custom adapter, e.g. new ArrayAdapter(context, R.layout.item);
This adapter would write the value of to string() of its data items to TextView with ID android.R.id.text1 inside that layout.

Mvvmcross: Cannot create MvxRecyclerAdapter in OnCreate of an Activity

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

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.

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

Android : how to set context to a view in layout xml?

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.

Categories

Resources