【问题标题】:I can't get data from model我无法从模型中获取数据
【发布时间】:2018-06-09 09:03:42
【问题描述】:

我创建了一个 POJO 模型并没有调用它。

这是运行它的代码。

DataObjectSelectGarage selectGarage = ...;
TextView.setText(selectGarage.getGaragename());

不知道这样能不能跑?

这是模型

public class DataObjectSelectGarage implements Serializable {
    @SerializedName("Garage_id")
    private int id;

    public int getGarage_id() {
     return id;
    }

    public void setgetGarage_id(String getGarage_id) {
     this.id = id;
    }

    @SerializedName("Garagename")
    private String name;

    public String getGaragename() {
     return name;
    }

    public void setGaragename(String name) {
     this.name = name;
    }
}

【问题讨论】:

  • 您是否在 oncreate 中对其进行了初始化? selectGarage = new DataObjectSelectGarage();

标签: java android dao pojo


【解决方案1】:
public void setgetGarage_id(String getGarage_id) {
     this.id = id;
    }

上面的set方法赋值id=id而不是id=getGarage_id

【讨论】:

    【解决方案2】:

    这样不行

    DataObjectSelectGarage selectGarage;
    
    TextView.setText(selectGarage.getGaragename());
    

    由于 DataObjectSelectGarage 未初始化。你需要先初始化它。

    其次,在您发布的代码中存在多个逻辑错误,例如您将 UserId 传递给 HTTP 请求参数而没有为其分配值。其次,没有试图将其呈现给用户的解析响应。

    @Override
    public void onResponse(String response) {
    /*DataObjectSelectGarage dataObjectSelectGarage = new 
    DataObjectSelectGarage();
    String test1 = dataObjectSelectGarage.getGaragename();*/
    tvTest.setText(selectGarage.getGaragename());
    Log.d("Selectgarage",response.toString());
    }
    

    您需要解析响应字符串,然后在初始化模型对象并在其中设置值后使用它在视图上显示数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 2018-04-16
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      相关资源
      最近更新 更多