我想问些什么,我想用输入的编辑文本设置“ Agama”。但是,结果是这样的错误:
java - 如何从Android中的嵌套对象访问setter?-LMLPHP

在我的活动中,我使用微调器获取agama值:

spAgama = findViewById(R.id.spfagama);
        ArrayAdapter<CharSequence> adapterAgama = ArrayAdapter.createFromResource(TambahDataKeluarga.this,R.array.agama, android.R.layout.simple_spinner_item);
        adapterAgama.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spAgama.setAdapter(adapterAgama);


如何将微调器值设置为setAgama?
这是我的familylistresponse.java;

package com.example.cobaakses.cobaarrayretrofit;

import com.example.cobaakses.Agama;
import com.google.gson.annotations.SerializedName;

public class familylistresponse {

    @SerializedName("noPegawai")
    private String noPegawai;

    @SerializedName("date_otor")
    private Object dateOtor;

    @SerializedName("keterangan")
    private String keterangan;

    @SerializedName("tgl_Lahir")
    private String tglLahir;

    @SerializedName("nama")
    private String nama;

    @SerializedName("agama")
    private Agama agama;

    @SerializedName("statusNikah")
    private String statusNikah;

    @SerializedName("tempatLahir")
    private String tempatLahir;

    @SerializedName("id")
    private int id;

    @SerializedName("golDarah")
    private String golDarah;

    public void setNoPegawai(String noPegawai){
        this.noPegawai = noPegawai;
    }

    public String getNoPegawai(){
        return noPegawai;
    }

    public void setDateOtor(Object dateOtor){
        this.dateOtor = dateOtor;
    }

    public Object getDateOtor(){
        return dateOtor;
    }


    public void setKeterangan(String keterangan){
        this.keterangan = keterangan;
    }

    public String getKeterangan(){
        return keterangan;
    }


    public void setTglLahir(String tglLahir){
        this.tglLahir = tglLahir;
    }

    public String getTglLahir(){
        return tglLahir;
    }

    public void setNama(String nama){
        this.nama = nama;
    }

    public String getNama(){
        return nama;
    }

    public void setAgama(Agama agama){
        this.agama = agama;
    }

    public Agama getAgama(){
        return agama;
    }

    public void setStatusNikah(String statusNikah){
        this.statusNikah = statusNikah;
    }

    public String getStatusNikah(){
        return statusNikah;
    }

    public void setTempatLahir(String tempatLahir){
        this.tempatLahir = tempatLahir;
    }

    public String getTempatLahir(){
        return tempatLahir;
    }

    public void setId(int id){
        this.id = id;
    }

    public int getId(){
        return id;
    }

    public void setGolDarah(String golDarah){
        this.golDarah = golDarah;
    }

    public String getGolDarah(){
        return golDarah;
    }

    @Override
    public String toString(){
        return
                "familylistresponse{" +
                        ",id = '" + id + '\'' +
                        ",date_otor = '" + dateOtor + '\'' +
                        ",agama = '" + agama + '\'' +
                        ",tgl_Lahir = '" + tglLahir + '\'' +
                        ",nama = '" + nama + '\'' +
                        ",keterangan = '" + keterangan + '\'' +
                        ",tempatLahir = '" + tempatLahir + '\'' +
                        "noPegawai = '" + noPegawai + '\'' +
                        ",golDarah = '" + golDarah + '\'' +
                        ",statusNikah = '" + statusNikah + '\'' +
                        "}";
    }
}


这是我的Agama.java:

package com.example.cobaakses;

import com.google.gson.annotations.SerializedName;

public class Agama{

    @SerializedName("id_Agama")
    private String idAgama;

    @SerializedName("agama")
    private String agama;

    public void setIdAgama(String idAgama){
        this.idAgama = idAgama;
    }

    public String getIdAgama(){
        return idAgama;
    }

    public void setAgama(String agama){
        this.agama = agama;
    }

    public String getAgama(){
        return agama;
    }

    @Override
    public String toString(){
        return
            "Agama{" +
            "id_Agama = '" + idAgama + '\'' +
            ",agama = '" + agama + '\'' +
            "}";
        }
}


而且,每当我尝试将数据输入数据库时​​,数据都不会插入数据库(我正在使用改造btw):(请有人帮我解决这个问题。谢谢

最佳答案

您在familylistresponse中的setAgama函数会收到一个Agama对象,因此您需要先创建一个Agama对象

Agama agama = new Agama()


现在您需要将想要从微调器中获得的字符串设置为该Agama对象

agama.setAgama(sp.getSelectedItem().toString().trim())


现在您的蜥蜴已经准备好设置为familylistresponse

f.setAgama(agama)


尝试对变量使用更好的名称,因为稍后在理解应用程序变大时的操作时会遇到一些麻烦。另外,请检查两个类中是否都具有setAgama函数。您应该在Agama类中使用不同的名称,因为您有一个内部有agama String的Agama对象。也许您可以将类定义为AgamaObject并在其中包含一个名为name的参数,该参数就是该Agama对象的名称

10-05 17:43