Reputation: 37
I Have a Query that I am calling a method which is present in an Adapter Class and I want to call it from another Adapter class.
I have done this before from Activity to Adapter class but not Adapter to Adapter
if(activity instanceof MainActivity){
((MainActivity)activity).TestMe();
}
above code is calling method from Activity to Adapter Class
below method I am calling it has Textviews
public void MajorMinorSum(){
int Major = 0;
int Minor = 0;
for (int i = 0; i < InlineGrid3ArrayList.size(); i++) {
int Maj = Integer.parseInt(InlineGrid3ArrayList.get(i).getMajor());
int Min = Integer.parseInt(InlineGrid3ArrayList.get(i).getMinor());
Major = Major + Maj;
Minor = Minor + Min;
}
txt_totalMajor.setText(String.valueOf(Major));
txt_totalMinor.setText(String.valueOf(Minor));
int AllowedMajor = Integer.parseInt(txt_AllowedMajor.getText().toString());
int AllowedMinor = Integer.parseInt(txt_AllowedMinor.getText().toString());
int RejectedMajor = Integer.parseInt(txt_RejectedMajor.getText().toString());
int RejectedMinor = Integer.parseInt(txt_RejectedMinor.getText().toString());
if (Major <= AllowedMajor && Minor <= AllowedMinor){
txt_WS_Result.setText("PASS");
txt_WS_Result.setTextColor(mContext.getResources().getColor(R.color.Green));
}else {
txt_WS_Result.setText("FAIL");
txt_WS_Result.setTextColor(mContext.getResources().getColor(R.color.Red));
}
}
But I have below Issue
if (mContext instanceof Inline_Grid_Defect_Adapter) {
((Inline_Grid_Defect_Adapter)mContext).MajorMinorSum();
}
where I am calling method Adapter to Adapter class
Upvotes: 0
Views: 47
Reputation: 159
You can call method by creating the class object in which the method is present and you can also call by by making that method static, but please remember to make method public in both the cases. here is an example.
class A{
public static void method1(){}
public void method method2(){}
}
In another class you can call both the method like this.
class B{
A a = new A();
a.method1();
a.method2();
// Or you can call static method without making object of class like below
A.method1();
}
Upvotes: 1
Reputation: 431
In first condition MainActivity is an activity but your adapter is not a Context. its a class that inheritance from recyclerview or etc.if i know what's the method job i can help you better. but you can call a method by making it public static
and call it like this:
Inline_Grid_Defect_Adapter.MajorMinorSum()
Upvotes: 0