Android 数据绑定问题,RecyclerView绑定数据使用的model中又嵌套Model

比如数据是这样的
class AModel{
BModel b;
}

class BModel{
CModel c;
}

class CModel{
getName();
}

然后传入Adapter的model是A,所以 取得数据时调用为:a.getB().getC().getName();
但是这中间很可能出现为null的情况,所以我的做法是,if(a != null && a.getB() != null && a.getB().getC() != null && !TextUtils.isEmpty(a.getB().getC().getName())),有更好的办法吗??

你是只需要C中的数据么?

我觉得可以在判断的getClassX的时候加一个if(X==null){return new ClassX}.最后你其它地方判断的时候,就只需要判断getName是否为空就行了。

try catch 捕获空指针异常就行catch中处理空指针情况!

这个我有想法,可以这样写:

class AModel{
BModel b = null;
BModel getBModel(){
    if(b == null)
        b = new BModel();
        return b;
}
}
class BModel{
CModel c = null;
CModel getCModel(){
    if(c == null)
        c = new CModel();
        return c;
}
}
class CModel{
private String name;
getName(){
    if(name==null)
    return "";
}
}