Reputation: 183
I am trying to make one of my function to be common for all my binding activities. i would like to hear some suggestions from you on this .
At present my code looks like :-
public static void stateDialog(Context context, String[] stateList, final FragmentSendAlertBinding binding) {
mStateDialog = new Dialog(context);
mStateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mStateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mStateDialog.show();
mStateDialog.setCancelable(false);
mStateDialog.setContentView(R.layout.dialog_select_state);
CardView cvState = (CardView) mStateDialog.findViewById(R.id.cv_state);
final ListView states = (ListView) mStateDialog.findViewById(R.id.countryList);
final TextView tvCancel = (TextView) mStateDialog.findViewById(R.id.tv_cancel);
final Animation myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce);
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20);
myAnim.setInterpolator(interpolator);
cvState.startAnimation(myAnim);
ArrayAdapter<String> itemList = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, stateList);
states.setAdapter(itemList);
states.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
binding.tvState.setText((String) arg0.getItemAtPosition(position));
mStateDialog.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mStateDialog.dismiss();
}
});
}
Can i use something like :
public static void stateDialog(Context context, String[] stateList, final ViewDataBinding<T> binding) {
}
stateDialog is an function created inside my Alert Class and i just want this function to be common for all my Binding activities .I do not want to create separate functions for my each binding activity.
so that my this function can be used for all my binding activities not just for FragmentSendAlertBinding
This may seems silly to you but i just want to know that is there any way to achieve this .
Thanks&Regards I have never created generic classes before , i just trying
Any help would be greatly Appreciated!!!.
Upvotes: 0
Views: 87
Reputation: 35549
Instead of making it generic i will suggest you to create interface for that
interface StateDialogListener{
public void onValueSelect(String value);
}
Your method will look
public static void stateDialog(Context context, String[] stateList, final StateDialogListener listener) {
mStateDialog = new Dialog(context);
mStateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mStateDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mStateDialog.show();
mStateDialog.setCancelable(false);
mStateDialog.setContentView(R.layout.dialog_select_state);
CardView cvState = (CardView) mStateDialog.findViewById(R.id.cv_state);
final ListView states = (ListView) mStateDialog.findViewById(R.id.countryList);
final TextView tvCancel = (TextView) mStateDialog.findViewById(R.id.tv_cancel);
final Animation myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce);
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20);
myAnim.setInterpolator(interpolator);
cvState.startAnimation(myAnim);
ArrayAdapter<String> itemList = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, stateList);
states.setAdapter(itemList);
states.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
listener.onValueSelect((String) arg0.getItemAtPosition(position));
//binding.tvState.setText((String) arg0.getItemAtPosition(position));
mStateDialog.dismiss();
}
});
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mStateDialog.dismiss();
}
});
}
And finally from wherever you are calling it
stateDialog(this,<your stateList>, new StateDialogListener(){
@Override
public void onValueSelect(String value){
binding.tvState.setText(value);
}
});
Upvotes: 1
Reputation: 43
Create a parent class for binding activity. Note that the class should have all the methods that you're going to use from the objects of its child classes. Then create other child classes that extends the parent binding class.
class ParentBinding{
public void method1();
public void method2();
public void method3();
}
Cusom Binding class
class BindingClass1 extends ParentBinding{
@override
public void method1(){
super();
}
@override
public void method1(){
super();
}
@override
public void method1(){
super();
}
}
Then your method can be written as
public static void stateDialog(Context context, String[] stateList, final ParentBinding binding) {
Upvotes: 0