My app contains three classes: main Activity, View_A, View_B.
View_B needs to access a non-static method of View_A. How to do that ?
Both View_A and View_B have been initialized in the main activity in the onCreate method.
Thanks.
It is siple.You jst need to create the reference of need class in you current class and call the method with referenceedObject.methodName();
you could store View_A as a public instance variable within your Main Activity. then pass the context of main activity to View_B. You can then access the instance of View_A via the context of main activity within View_B.
Here's basically what i mean
This is in your main Activity:
Context context;
public View_A viewA;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
viewA = new View_A();
View_B viewB = new View_B(context);
}
it would probably be better using a getter method to get the viewA from context.
Here is View_B example class:
class View_B {
Context activityContext;
// constructor
View_B (Context _c)
{
activityContext = _c;
viewA = activityContext.viewA;
}
}
that is basically what i mean, but as i said in my comment, Teds solution seems more elegant.
I think he meant something more like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
View_A viewA = new View_A();
View_B viewB = new View_B();
// create a setter method in viewB class to set viewA instance into it after viewA & viewB are created. or i guess you could pass viewA to viewB in the constructor of viewB
viewb.setViewA(viewA);
}
I hope something from that can help you out
Basically, you need a reference to View_A inside View_B. One way to do that is to define a View_A variable in View_B that is accessible to the activity class. In onCreate, just set the variable to the instance of View_A once both View_A and View_B have been created.
Related
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
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 have an A activity that uses a setContentView(R.layout.activityA) method to set a layout. An activityA layout consists of a customView. My customView has a bunch of setters and getters. How can I access them from A activity? When I create an instance of customView in an acitivity A then it works but the customView is created twice: once from the setContentView and the second time when I create a new instance of it. Is there another way of accessing those method? Please advise. Thanks.
Have you tried something like this in your Activity's code:
#Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout.activityA);
CustomView customView = (CustomView)findViewById(R.id.customviewId);
Something x = customView.someGetterMethodX();
...etc...
}
You can use Java Reflection to read attributes and call methods.
You don't have to create it twice, simply find your custom view id and assign it to a CustomView reference. Something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theLayout);
CustomView customView = (CustomView) findViewById(R.id.customViewId);
customView.setSomething(someValue);
}
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.
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(...)