有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当从EditText获取字符串时,java无法通过setter设置类对象的参数,但当传递硬编码字符串时也可以这样做

自定义类代码:

package com.example.accounts;
public class PartyModel {

    private int parId;
    private String parName;
    private String parType;
    private String parOwner;
    private String parOwnerContact;

    public PartyModel() {
            }

    public PartyModel(int parId, String parName, String parType, String parOwner, String parOwnerContact) {
        this.parId = parId;
        this.parName = parName;
        this.parType = parType;
        this.parOwner = parOwner;
        this.parOwnerContact = parOwnerContact;
    }

    public int getParId() {
        return parId;
    }

    public void setParId(int parId) {
        this.parId = parId;
    }

    public String getParName() {
        return parName;
    }

    public void setParName(String parName) {
        this.parName = parName;
    }

    public String getParType() {
        return parType;
    }

    public void setParType(String parType) {
        this.parType = parType;
    }

    public String getParOwner() {
        return parOwner;
    }

    public void setParOwner(String parOwner) {
        this.parOwner = parOwner;
    }

    public String getParOwnerContact() {
        return parOwnerContact;
    }

    public void setParOwnerContact(String parOwnerContact) {
        this.parOwnerContact = parOwnerContact;
    }
}

活动代码(其中值传递给PARTYMODEL类的SETTER)在此活动中,我将PARTYMODEL对象添加到数据库中,当值取自硬编码字符串时,它工作正常,当值取自editText时,值未设置:

package com.example.accounts;

import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;

public class AddNewParty extends AppCompatActivity {

    EditText etPartyName,etPartyType,etPartyOwner,etPartyOwnerContact;
    Button btnCreate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_party);


        etPartyName=findViewById(R.id.et2PartyName);
        etPartyType=findViewById(R.id.et2PartyType);
        etPartyOwner=findViewById(R.id.et2PartyOwner);
        etPartyOwnerContact=findViewById(R.id.et2OwnerContact);
        btnCreate=findViewById(R.id.btn2Create);

        MyDbHandler db = new MyDbHandler(AddNewParty.this);

        PartyModel partyModel = new PartyModel();

        partyModel.setParName(etPartyName.getText().toString());
        partyModel.setParType("Trader");
        partyModel.setParOwner("Mr. XYZ");
        partyModel.setParOwnerContact("9810454563");


        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.addParty(partyModel);
                Intent intent = new Intent(AddNewParty.this,com.example.accounts.MainActivity.class);
                startActivity(intent);
            }`
        });
     }
}

共 (1) 个答案

  1. # 1 楼答案

    点击按钮后从editText获取数据

     btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) {
            //if edit texts are not empty ,set it in model class
            if(ePartyName.getText().toString().isEmpty() || 
             netPartyType.getText().toString().isEmpty()|| 
             etPartyOwner.getText().toString().isEmpty() || 
             etPartyOwnerContact.getText().toString().isEmpty())
             return;
        
        else{
             PartyModel partyModel = new PartyModel();
            
               partyModel.setParName(etPartyName.getText().toString());
               partyModel.setParType( netPartyType.getText().toString());
               partyModel.setParOwner(etPartyOwner.getText().toString());                
               
               partyModel.setParOwnerContact(etPartyOwnerContact.getText().toString());
            
            
               db.addParty(partyModel);
             Intent intent = new Intent(AddNewParty.this,com.example.accounts.MainActivity.class);            
                            startActivity(intent);
                        }`}
                    });
                 }