Keith
Keith

Reputation: 4204

How do I inflate a custom view as itself?

I'm using a custom view (RouteView) in an ArrayAdapter and trying to use a custom view here so I can do some logic on the object, but for some reason it seems to be invisible! I can interact with it, but nothing is visible.

Here's my RouteView class:

public class RouteView extends RelativeLayout {

public RouteView(Context context){
    super(context);
    init();
}
private void init(){

    inflate(getContext(), R.layout.route_item, this);
}
public void setFrom(String fromText){
    TextView from = (TextView) findViewById(R.id.from);
    from.setText(fromText);
}
public void setTo(String toText){
    TextView to = (TextView) findViewById(R.id.to);
    to.setText(toText);
}
public RouteView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

}

And here's how I'm constructing it:

private class RoutesAdapter extends ArrayAdapter{
    public RoutesAdapter(Context context, int resource, ArrayList<Route> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        RouteView v;
        if(convertView == null){
            v = new RouteView(getContext());
        } else {
            v = (RouteView) convertView;
        }

        Route route = (Route) getItem(position);
        v.setFrom(route.getFrom());
        v.setTo(route.getTo());

        return v;
    }
}

My XML root element is a <merge> element that was a RelativeLayout. Does anyone know what I'm doing wrong here?

Thanks!

Upvotes: 1

Views: 65

Answers (1)

subhash
subhash

Reputation: 2707

yes as @kingfisher-phuoc said your:

@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

}

try calling super:

 @Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
    super.onLayout(b, i, i1, i2, i3);
    ... //your code
}

or dont override this method

Upvotes: 1

Related Questions