如何制作验证微调器适配器?

时间:2018-10-31 17:05:04

标签: android android-spinner android-adapter android-studio-3.0

没有错误,但是问题是当用户未选择任何学费包时,我想敬酒一条消息“选择学费包”。

addRegistration方法中的验证代码:-

else if(mTuitionPackage.getSelectedItemPosition() == 0){
    Toast.makeText(RegisterActivity.this, "Select the tuition package.", Toast.LENGTH_SHORT).show();
    return;
}

旋转编码适配器:-

if(mAdvertisement != null){
        mTuitioName.setText(mAdvertisement.getAdstuitioname());

        ArrayList<TuitionPackage> tuitionPackages = mAdvertisement.getTuitionPackages();

        //Instantiate the custom list adapter
        PackageSpinnerAdapter adapter = new PackageSpinnerAdapter(this, tuitionPackages);
        mTuitionPackage.setAdapter(adapter);
        mTuitionPackage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                TuitionPackage tuitionPackages = (TuitionPackage) adapterView.getItemAtPosition(i);
                mTotalAmount.setText("RM "+tuitionPackages.getPrice());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                //Do nothing
            }
        });
 }

因此,如下面的界面输出所示,我想将位置0选择“ Choose Tuition Package”。这样我们就可以进行验证了吗?当用户没有选择任何补习包时,它将提示“请选择补习包”。 enter image description here

TuitionPackage类编码:-

public class TuitionPackage implements Serializable {

private String packageStatus;
private String academicLevel;
private String subjects;
private String price;

public TuitionPackage() {
    this.packageStatus = "";
    this.academicLevel = "";
    this.subjects = "";
    this.price = "";
}

public String getPackageStatus() {
    return packageStatus;
}

public void setPackageStatus(String packageStatus) {
    this.packageStatus = packageStatus;
}

public String getAcademicLevel() {
    return academicLevel;
}

public void setAcademicLevel(String academicLevel) {
    this.academicLevel = academicLevel;
}

public String getSubjects() {
    return subjects;
}

public void setSubjects(String subjects) {
    this.subjects = subjects;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}
}

1 个答案:

答案 0 :(得分:1)

首先将默认值添加到tuitionPackages列表中。

     ArrayList<TuitionPackage> tuitionPackages = new ArrayList<>();
     TuitionPackage defaultTutionPack = new TuitionPackage();

     tutionPackages.add(defaultTutionPack )
     //after that add other values
     tutionPackages.addAll(mAdvertisement.getTuitionPackages())