android-context

Android DataBinding where to get context?

好久不见. 提交于 2019-11-27 03:43:07
问题 I have TextView for showing time. I want to use Android's DataBinding plugin. For formatting time I am using DateUtils.formatDateTime(context, int, int) method which takes Context instance. Is it possible to get context include element? Or do I have to use old school way? Thanks 回答1: Thought I should answer instead of putting in a comment. You'll have more options when rc2 is released. In rc1, you can pass the context in a variable to the Binding, then pass it as a parameter to the method.

Get context inside onClick(DialogInterface v, int buttonId)?

跟風遠走 提交于 2019-11-27 03:42:26
Getting the context inside onClick(View view) , the callback for a button's onClickListener() , is easy: view.getContext() But I can't figure out how to get the context inside onClick(DialogInterface v, int buttonId) , the callback for a dialog 's onClickListener public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener Is this possible? You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous class. If you're in an activity you can use MyActivity.this as the context. Edit - since your Activity is

Android: why must use getBaseContext() instead of this

时间秒杀一切 提交于 2019-11-27 02:56:11
this often to reference to current context. But, at some case, why we must use getBaseContext() instead of this . (It means when use this will notice error). Here is my example: Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?>arg0, View arg1, int arg2, long arg3){ Toast.makeText(getBaseContext(),"SELECTED", Toast.LENGTH_SHORT).show(); //this line } At above code, when I change getBaseContext() to this will receive error. Who can explain for me

Call to getLayoutInflater() in places not in activity

别来无恙 提交于 2019-11-27 02:32:50
What does need to be imported or how can I call the Layout inflater in places other than activity? public static void method(Context context){ //this doesn't work the getLayoutInflater method could not be found LayoutInflater inflater = getLayoutInflater(); // this also doesn't work LayoutInflater inflater = context.getLayoutInflater(); } I am able to call getLayoutInflater only in activity, is that an restriction? What if I want to create custom dialog and I want to inflate view for it, or what if I want to have Toast message with custom view that is shown from a service, I only have the

How to get a context in a recycler view adapter

青春壹個敷衍的年華 提交于 2019-11-26 23:59:16
问题 I'm trying to use picasso library to be able to load url to imageView, but I'm not able to get the context to use the picasso library correctly. public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> { private List<Post> mDataset; // Provide a reference to the views for each data item // Complex data items may need more than one view per item, and // you provide access to all the views for a data item in a view holder public class ViewHolder extends RecyclerView

Android Intent Context Confusing

隐身守侯 提交于 2019-11-26 23:56:34
问题 Can somebody explain this to me please : Intent intent = new Intent(Context, AlarmReceiver.class); I never understood and I seriously think I never will if somebody doesn't try to explain this to me in depth. This whole context thing is so confusing to me. Sometimes it works like this : Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); Sometimes it wont work like that but it accepts only : Intent intent = new Intent(context, AlarmReceiver.class); Sometimes its : Intent intent

Change app language in android 5.0 doesn't work

[亡魂溺海] 提交于 2019-11-26 22:03:37
问题 I'm using this code below to change my app language on button click (changing from french to english for example), it's works fine on android 4.0 + but on 5.0 it doesn't. Locale localeEn = new Locale("en_US"); Locale.setDefault(localeEn); Configuration configEn = new Configuration(); configEn.locale = localeEn; getApplicationContext().getResources().updateConfiguration(configEn, null); this.recreate(); Any clues why please? edit : this is my manifest ( with android:configChanges ) <activity

Why is my context in my Fragment null?

妖精的绣舞 提交于 2019-11-26 22:01:57
问题 I have got a question regarding the usage of context in a fragment. My problem is that I always get a NullpointerException. Here is what i do: Create a class that extends the SherlockFragment. In that class I have an instance of another Helper class: public class Fragment extends SherlockFragment { private Helper helper = new Helper(this.getActivity()); // More code ... } Here is an extract of the other Helper class: public class Helper { public Helper(Context context) { this.context =

get Context in non-Activity class [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-26 21:59:58
This question already has an answer here: How to retrieve a context from a non-activity class? 4 answers In an android Application, is there any way to get the context in android in a non-activity class if the activity class name is known? If your class is non-activity class, and creating an instance of it from the activiy, you can pass an instance of context via constructor of the later as follows: class YourNonActivityClass{ // variable to hold context private Context context; //save the context recievied via constructor in a local variable public YourNonActivityClass(Context context){ this

What exactly does using the Application Context mean?

做~自己de王妃 提交于 2019-11-26 20:45:34
问题 I'm new to this and I'm sorry if this is a really dumb question. I'm just trying to clarify things. My book says I can retrieve application context for process by using the getApplicationContext() method. I just really don't know where to type this or what to do with any of it. I can go to the hierarchy but what do I do with all the script there. Also where would I write Activity Callbacks, in the main.xml? An exercise wants me to add a logging tag to my project but I'm not sure how to do