Java Android,遍历所有获取器

我有一个如下模型:

public class Foo{

    private double h00;
    private double h01;
    private double h02;
    private double h03;

    public Shifts() {
    }

    public Shifts(double h00, double h01, double h02, double h03) {
        this.h00 = h00;
        this.h01 = h01;
        this.h02 = h02;
        this.h03 = h03;
    }

    public double getH00() {
        return h00;
    }

    public void setH00(double h00) {
        this.h00 = h00;
    }

    public double getH01() {
        return h01;
    }

    public void setH01(double h01) {
        this.h01 = h01;
    }

    public double getH02() {
        return h02;
    }

    public void setH02(double h02) {
        this.h02 = h02;
    }

    public double getH03() {
        return h03;
    }

    public void setH03(double h03) {
        this.h03 = h03;
    }
}

I'm calling this model in a recycle adapter to update the UI and on the onBindViewHolder, I do like this:

holder.h00.setText(fooList.get(position).getH00()));
holder.h01.setText(fooList.get(position).getH01()));
holder.h02.setText(fooList.get(position).getH02()));
holder.h03.setText(fooList.get(position).getH03()));

实际上,此模型有很多吸气剂和吸气剂,而我试图遍历它们,以便可以执行以下操作:

for (int i = 0; i < holder.shift_layout.getChildCount(); i++) {
                //shift_layout is the Layout which holds all the views
                View v = holder.shift_layout.getChildAt(i);
                if (v instanceof TextView) {

                    v.setText(Hour(fooList.get(position).getGetter()));
                }
            }

有没有一种方法可以使用Java反射或任何方法遍历模型的所有getter,然后调用它们?

查看SO,我发现了一些答案,但是调用吸气剂没有成功。

评论
  • 买依薇
    买依薇 回复

    像这样:

    final Object obj;    
    for (Method m : obj.getClass().getMethods())
        if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
          final Object object = m.invoke(obj);
          // do something
        }
    }