how to get context in to other class - android

i have public class ExperimentAllInOneActivity extends Activity implements OnClickListener on each button click listener i want to go some other class like public class TemperatureStrategy and perform all logic calculation in this class but main thing is that i have class like public class DialogDisplay extends Dialog implements OnItemSelectedListener in this class a custom dialog is created. Now I have question is that on each button onclick listener I want to call(display) this custom dialog class in, spinner class and like that type of element, and all logic performed with class TemperatureStrategy. How to build constor and pass context of all class?

you can do it in 2 ways AFAIK:
1. Add a context argument in your function like this:
public static void display (Context context) {
// Do your action using action...
2. Add a context argument to your class constructure and use this context in all your functions.

If your onClickListeners are declared inside the Activity (I suppose they are), then you can call getApplicationContext() anywhere inside the Activity class, as opposed to use the 'this' keyword that would refer to the onClickListener itself.

You can place the context in the .Tag property that you pass into your button event and pull it back out.

Related

I have an activity with recycler view adapter class, when button clicked in adapter class I want to refresh the main activity

I have an activity with RecyclerView.Adapter class. RecyclerView contains button. When click that button I want to change a value defined in main activity and display it in TextView that also defined in main activity , otherwise I want to refresh the activity.
I already tried some code like:
Android, How to call onCreate() explicitly from other method?
How to restart the onCreate function
YourActivity extends Activity implements YourAdapter.ClickCallback{
#override
oncreate(Bundle bundle){
// initialize views
YourAdapter adapter = new YourAdapter(this);
// do something with your adapter
}
#Override
public void updateView(){
//update your views here
}
}
//In Recycler Adapter pass ClickCallBack As parameter
class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ClickCallBack clickCallBack;
public YourAdapter(ClickCallBack clickCallBack){
this.clickCallBack=clickCallBack;
}
//ClickCallback Interface
interface ClickCallBack {
void updateView();
}
//In your ButtonClick
clickCallBack.updateView();
}
You can also add parameters in updateView method.
Pass an instance of those TextView through constructor and update the value there in the Adaptor. That would be the easiest way.
You don't need to recreate Activity in this case. The best solution would be:
Declare an interface.
Implement it in Activity(here implement your updates to TextView, etc.).
Pass object that implements the interface to your adapter.
Call the method on this interface in adapter.
Here is example:
https://stackoverflow.com/a/32720879/2528967

How to refresh a view in main activity from an adapter?

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.

how to get the xml id used in first class to use in second class in android

I'm getting the android xml id in first Activity
(e.g)
setContentView(R.layout.sample);
button1=(Button)findViewById(R.id.b_one);
button2=(Button)findViewById(R.id.b_two);
Now the problem is how can i use the same button in second Activity without using findViewById().
(i.e) I don't want to access xml in second Activity rather than that i have to access those button ID from the first Activity itself.
I'm trying this to create a common header.
Please help me.
You can't, because your buttons from two different activities will not share the same instance.
What you can do instead, if you want to have some common code for your header is to delegate this code in a static method of a third class. For example :
public class MyHeader {
private Button button1,button2;
public MyHeader(Activity source) {
this.button1 = (Button)source.findViewById(R.id.b_one);
this.button2 = (Button)source.findViewById(R.id.b_two);
// ...
}
public Button getHeaderButtonOne() { return button1; }
// And so on...
}
and in your activities, keep an instance of this class (initialized with :
private MyHeader header;
// ... in onCreate() method
header = new MyHeader(this);

Android findViewById throws NullPointerException

I have a ViewFlipper defined in a separate class. It looks like this:
public class Flipper extends Activity {
public ViewFlipper view_flipper;
/* Constructor */
public Flipper(int flipper_id) {
view_flipper = (ViewFlipper) findViewById(flipper_id);
}
...
Then in another Activity's onCreate() method I am instantiating the Flipper like this:
private Flipper flipper;
flipper = new Flipper(R.id.login_screen_flipper);
However, I get a NullPointerException in the constructor of Flipper. When I debug, I see that flipper_id has a valid id. What am I doing wrong?
You can't create Activities like this.
To create an activity you must do it using Intents. And you have to define the layout in an XML file and attach it using setContentView(R.layout.layout_name);
Have a look at this to start learning: https://developer.android.com/training/index.html
UPDATE:
OK, it's unclear of what you are accomplishing with the info of your question.
If you are extending the behaviour of ViewFlipper I suggest you extend it:
package your.package;
public class MyFlipper extends ViewFlipper {
// ...
}
Then use it in your xml like this:
<your.package.ViewFlipper
[...] parameters [...] />
And finally when you implement it you can do it like this:
public class YourActivity extends Activity {
public ViewFlipper viewFlipper;
#Override
protected void onCreate (Bundle savedInstanceState) {
viewFlipper = (ViewFlipper) findViewById(R.id.login_screen_flipper);
}
// ... more activity stuff
}
PS: It's good custom to use the lowerCamelCase notation in Java member variables (or fields in Java)
When you instantiate Flipper in another activity, findViewById is called in the constructor of Flipper. It is a method from Flipper class as it extends Activity and surely its call is not valid in the constructor because you do not set content view as it is normally done in Activity.onCreate. Flipper should not extend Activity and you should pass ViewFlipper in a constructor not id.

change background color of textview from another class

Say for example I have a textview in class A,
and I want to change background color of textview from class B through a method...
how can I do it?
Make sure the tvView being passed is associated with a valid control (ie, findViewById() has been called before calling this function)
class B
{
public void changeBG(TextView tvView)
{
tvView.setBackgroundColor(Color.BLUE);
}
};
I think, you better have a public method changeBG in class A.
call that changeBG from Class B.

Categories

Resources