简体   繁体   中英

Android How can i generalize a Class for reuse

In my android code i needed to place an edittext in a recyclerview and save the value entered using TextWatcher. For that purpose i used a customized TextWatcher as suggested here.

private class MyCustomEditTextListener implements TextWatcher {       
int position;
public void updatePosition(int position)
{ 
     this.position=position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2,
int i3) {}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2,
int i3) {}

@Override
public void afterTextChanged(Editable editable) {}}

The updatePosition is called from onBindViewHolder.. and i save the edittext contents to data model from the afterTextChanged.

My situation now is i use the MyCustomEditTextListener as inner class. But the same usage logic is required in multiple activities. How can i rewrite the class so that i dont have to duplicate it in every recycler adapter.

In the afterTextChanged() method i use something like follows

modelList.get(position).setQuantity(editable.toString());

where modelList is an ArrayList of 'Model' objects. But my issue becomes that the Model class is different in each Adapter.

You can write a static function and return your TextWatcher

  public static TextWatcher getTextWatcher(){
    return new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {}

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2,
                                  int i3) {}

        @Override
        public void afterTextChanged(Editable editable) {}
    };

}

And you can use it with YourClassName.getTextWatcher()

Somehow you will implement the textwatcher in the view holder class only. So better you can write a BaseViewHolder class which can or cannot be abstract it is based on your need, if you want to fetch any data from the child class means then you can declare a abstract method and make the class as abstract . Make the BaseViewHolder class as parent class for all the ViewHolder class for which you are going to implement this logic. This will be helpful if you are going to implement this logic in recyclerview .

If you want to implement this logic in both activities and recyclerview means, then you are supposed to write a BaseActivity class which will be the parent class for your Activity . As I said this class can or cannot be abstract , it is upto your need.

Put your code in that Base class ad access it. By this way the reusability can be achieved and if you change in single place, that will be affected in all other classes. By this way you won't face a scenario, it is working fie in acitivty A but not in activity B.

Hope this is helpful:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM