1

In case you don't know what would go in these classes then please look here! Also, I am not 100% sure this would work yet, I am testing this out


I am currently working on creating a simplified base class that would simplify using custom xml attributes in class AttributeSet's

basically, this is what I am kind of look for as a final result...

public class SimpleViewImplementation extends SimpleView<LinearLayout> {

    // List of members here
    private String value;

    public SimpleViewImplementation(Context context) {
        super(context);
    }
    public SimpleViewImplementation(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void setFromStyledAttributes(TypedArray attr) {

        // Set conditions for each member here via TypedArray (use setters)
        setValue(attr.getString(R.styleable.SimpleViewImplementation_value));

    }

    @Override
    protected void initView() {

        // Set initial conditions for each member here
        this.value = "this is the default value!";

    }

    // Getters & Setters here for members
    public String getValue() { return this.value; }
    public void setValue(String value) { 

        this.value = value;
        this.updateViewOnSet();
    }

}

and this is the "base" class that does all the magic. The problem is really the class "signature". I need it to extend type T. Either I missed how to do that in my research online, or it cannot be done. If it cannot be done, then are they any suggestions to get somewhat of my results above. In case you don't know what would go in these classes then please look here!

public abstract class SimpleView<T> {   // I would like this class to extend Type T. ie SimpleView<LinearLayout> would extend this class to be a LinearLayout...getting rid of compile-time errors below
    //                             ^ can I put anything here????

    public SimpleView(Context context) {

        super(context); // CTE (Compile-time error)
        initView();

    }

    public SimpleView(Context context, AttributeSet attrs) {

        super(context, attrs);  // CTE
        initView();

        TypedArray attr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawerSongDetail, 0, 0);

        try {

            this.setFromStyledAttributes(attr);

        } finally { 

            attr.recycle(); 

        }

    }

    // Sets all members based on AttributeSet parameter
    abstract protected void setFromStyledAttributes(TypedArray attr);

    // Sets all initial values of members
    abstract protected void initView();

    private void updateViewOnSet() {

        this.requestLayout();   // CTE
        this.invalidate();  // CTE

    }
}
Community
  • 1
  • 1
Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58

1 Answers1

0

Here is an example of how it can be done: http://www.lukehorvat.com/blog/android-seekbardialogpreference/

Edit:

Would would be missing in a setup like this?

public abstract class SimpleView  {  

private View view;

public SimpleView(Context context, View view) {

    if (view == null || !(view instanceof View)) {
        throw new RuntimeException("SimpleView expects an instance of View");
    }

    this.view = view;

    initView();

}

public SimpleView(Context context, View view, AttributeSet attrs) {

    if (view == null || !(view instanceof View)) {
        throw new RuntimeException("SimpleView expects an instance of View");
    }

    this.view = view;

    initView();

    TypedArray attr = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DrawerSongDetail, 0, 0);

    try {

        this.setFromStyledAttributes(attr);

    } finally { 

        attr.recycle(); 

    }

}

// Sets all members based on AttributeSet parameter
abstract protected void setFromStyledAttributes(TypedArray attr);

// Sets all initial values of members
abstract protected void initView();

protected void updateViewOnSet() {

    view.requestLayout();  
    view.invalidate(); 

}
}

As I think you would could use this to:

public class SimpleViewImplementation extends SimpleView {

// List of members here
private String value;

public SimpleViewImplementation(Context context) {
    super(context, new RelativeLayout(context));
    // Define your View here (demonstrated with RelativeLayout)
}

public SimpleViewImplementation(Context context, View view) {
    // Predefined view
    super(context, view);
}

public SimpleViewImplementation(Context context, AttributeSet attrs) {
    super(context, new LinearLayout(context), attrs);
    // Define your View here (demonstrated with LinearLayout)
}

public SimpleViewImplementation(Context context, View view, AttributeSet attrs) {
    super(context, view, attrs);
    // Predefined view
}

@Override
protected void setFromStyledAttributes(TypedArray attr) {

    // Set conditions for each member here via TypedArray (use setters)
    setValue(attr.getString(R.styleable.SimpleViewImplementation_value));

}

@Override
protected void initView() {

    // Set initial conditions for each member here
    this.value = "this is the default value!";

}

// Getters & Setters here for members
public String getValue() { return this.value; }
public void setValue(String value) { 

    this.value = value;
    this.updateViewOnSet();
}

}
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • That is an example that is comparable to the link I share at the very beginning of the comment. I know how to do that. I am looking at **abstracting** out the extra detail, and trying to make the class as simple as possible. I do like this link to show a good reference to how it is normally done. My question is specifically dealing with generics and extending the base class to that generic – Christopher Rucinski Sep 27 '13 at 01:16
  • Ok, missed that difference, ill take a look tommorow if not answered by then – cYrixmorten Sep 27 '13 at 01:59
  • hmmm, I will have to look at that more. It does seem like it could be would I kind of was looking for. I came up with a slightly undesirable solution to this problem last night that appears to work, but I don't like the overhead of it. This solution of yours might fix that. Anyway, I will check it out later today. – Christopher Rucinski Sep 27 '13 at 19:02
  • Fair enough, I hope it helps you in the right direction. Looking into it I at least learned to crete generics :D although that was not possible for this use case. – cYrixmorten Sep 27 '13 at 19:10
  • I have not been able to make yours or mine work yet. I will start from the very beginning. Something like your original link, and see if I can extend it from there again, piece-wise – Christopher Rucinski Sep 28 '13 at 20:50
  • Ok, bummer it did not work. Good luck and by all means share if you find a good solution :-) – cYrixmorten Sep 28 '13 at 21:08
  • Would you mind telling what problems you encountered? Because I made sure that it would atleast compile before posting the answer, so honestly thought it would work. My only concern was if it would grant the level of abstraction you seeked. – cYrixmorten Sep 29 '13 at 09:21