android-context

context parameter for custom adapter - getApplicationContext() fails but 'this' works

吃可爱长大的小学妹 提交于 2019-12-06 15:31:38
I have simple code snippet to implement custom list view. In this code, I have CustomAdapter class which extends ArrayAdapter : CustomAdapter adapter = new CustomerAdapter(getApplicationContext(),R.layout.listview_item_row, weather_data); The constructor of CustomAdapter is as below: public CustomerAdapter(Context context, int layoutResourceId, weather[] data) { super(context, layoutResourceId, data); mlayoutResourceId = layoutResourceId; mcontext = context; mdata = data; } If I write this, the logcat shows following error: Java.lang.ClassCastException: android.app.Application context can not

Expanding in Context in a ListView - Android

大城市里の小女人 提交于 2019-12-06 14:51:26
问题 I have a listView, one component of the row is a TextView. By default, I wish to show only 2 rows of text in the TextView. But if the user taps on the TextView I wanted to the textView to expand in context. I actually have this portion working, but I want to have a more content indicator : My current implementation (Below) has it's own issues w/ not collapsing if the list view is scrolled, but I will handle that by storing the values for each cursor record in some collection.. I tried using

how to use SharedPreferences outside of oncreate?

独自空忆成欢 提交于 2019-12-06 12:49:47
How to use SharedPreferences in a class without oncreate ? I get null pointer when accessing it. public class Ftr extends Activity { SharedPreferences preferences; Context ab=this; public void ft() { preferences = PreferenceManager.getDefaultSharedPreferences(ab); String result = preferences.getString("F",""); } } I'm calling Function ft() from another activity Ftr is just a class not an activity. How can I use SharedPreferences in this condition? You can take in Common method or Utils So use in All Method. public static void SaveInPreference(Context mContext, String key, String objString) {

Difference between context [duplicate]

廉价感情. 提交于 2019-12-06 11:22:30
This question already has answers here : Closed 7 years ago . Possible Duplicate: Android - what's the difference between the various methods to get a Context? I want to know what's the difference between using this , ClassName.this , getApplicationContext() or myContext ? What are the effects of using each as context in the Toast below? public class ClassName extends Activity { final ClassName myContext = this; ... public void onCreate(Bundle savedInstanceState) { ... button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { Toast.makeText(getApplicationContext(), "This

Android: should I maintain a reference to the activity inside a recyclerview or is there another way?

烈酒焚心 提交于 2019-12-06 07:19:54
问题 I need to use context methods within the onBindViewHolder (a standard example might be something as common as getString or getColor). Until now I've passed the context to the constructor for the recyclerview and maintained a reference in a variable, however this seems to me to be bad practice. Is there a way of getting context dynamically from inside a recyclerview without storing it as a variable? public SomeRecyclerViewClass(Activity activity) { this.parentActivity = activity; } 回答1: I

Kotlin Android Fragment recyclerView and context issue

社会主义新天地 提交于 2019-12-06 06:26:18
问题 I would like to create an recyclerView in a fragment, but it shows an error " java.lang.IllegalStateException: recylerView_Main must not be null at com.gph.bottomnavigation.FragmentMe.onCreateView(FragmentMe.kt:28)" Question 1) Please kindly help to solve this issue. Question 2) I created an recyclerView only in a empty project without any fragment, it is working properly. But the same code is no working in Fragment, it shows error so I change "recylerView_Main.layoutManager =

How to Start an Intent from a contained class of an Activity

蓝咒 提交于 2019-12-06 05:44:33
I'm looking for the best way to start an intent from a class that is not an Activity, but is a contained object of an Activity class. For example the Activity Class: Class MainActivity extends ListActivty { ... TestLauncher tester; } and the class that I want to start the intent from: Class TestLauncher { public TestLauncher () { //Code to create an intent needs a Context //Intent i = new Intent(Context, class) //Code to start activity needs to be called with an Activity //Activity.StartActivity(i); } } What is the best way to do this architecturally? Should I pass MainActivity as a parameter

Memory-leak free Singleton with context

元气小坏坏 提交于 2019-12-06 04:50:10
I am trying to implement the following singleton pattern: SingletonClass.getInstance(context).callMethod() While there are a variety of tutorials that explain how to make singletons in Kotlin, none of them address the fact that holding a context in a static field will cause memory leaks in Android. How do I create the above pattern without creating a memory leak? Update: Here is my implementation of CommonsWare's solution #2. I used Koin. Singleton class: class NetworkUtils(val context: Context) { } Application Class: class MyApplication : Application() { val appModule = module { single {

Contexts, AsyncTask and rotation changes

感情迁移 提交于 2019-12-06 04:44:30
问题 Is it a good practice to use getApplicationContext() working with AsyncTask in order to do not have to attach and detach the Activity to avoid memory leaks when rotation changes occur and the Activity is destroyed ? I thing it should be correct, as I actually need a Context that depends on the hole application, not the Activity itself. And what is more, in those cases in which is better to use the Activity as context (because you need access to the Activity showing)... Instead of detaching it

Accessing strings.xml from ViewModel

﹥>﹥吖頭↗ 提交于 2019-12-06 02:59:55
I am using Dagger 2 DataBindng and the new Android Lifecycle components, which have ViewModels . Inside my ViewModel how could I get access to my strings.xml? I was thinking at first, to inject a Context into the viewModel , however, this will just leak memory. Are there any other ways? There is an AndroidViewModel , which receives Application instance as parameter. From docs: Application context aware ViewModel . Subclasses must have a constructor which accepts Application as the only parameter. You can retrieve a string from strings.xml using that parameter. The repo in the link, however