如何在spinner android中显示数据?

时间:2014-07-15 10:18:35

标签: android spinner

我正在使用从数据库动态填充的微调器

这是我的代码

 <Spinner
                android:id="@+id/spnOrdrPrdBrand"
                style="@style/ButtonStyleSpinner"
                android:layout_marginTop="5dp"
                android:hint="@string/select"
                android:paddingLeft="5dp" />

    List<Brand> brandList = new ArrayList<Brand>();

    if(!custId.equals("0")){
                    brandList = cCon.getBrandList(custId);                      
                }
                // Sorting
                //Collections.sort(brandList);

                //Brand Lst
                ArrayAdapter<Brand> brandAdp = new ArrayAdapter<Brand>(this,android.R.layout.simple_spinner_item,brandList.toArray(new Brand[brandList.size()]));
                brandAdp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spnOrdrPrdBrand.setAdapter(brandAdp);

数据是brandList对象中的数据 但在填充时显示我的对象名称而不是实际的品牌名称。 我正在使用

public class Brand implements Comparable<Brand>{
// private variables
protected int brandId;
protected String brandNm;

// Empty constructor
public Brand() {

}

// constructor
public Brand(int brandId, String brandNm) {
    this.brandId = brandId;
    this.brandNm = brandNm;     
}

/**
 * @return the brandId
 */
public int getBrandId() {
    return brandId;
}

/**
 * @param brandId the brandId to set
 */
public void setBrandId(int brandId) {
    this.brandId = brandId;
}

/**
 * @return the brandNm
 */
public String getBrandNm() {
    return brandNm;
}

/**
 * @param brandNm the brandNm to set
 */
public void setBrandNm(String brandNm) {
    this.brandNm = brandNm;
}

@Override
public int compareTo(Brand another) {
    if (this.brandNm.equals(another.brandNm)) {
        return ((Integer.valueOf(this.brandId)).compareTo(Integer.valueOf(another.brandId)));
    } else {
        return this.brandNm.compareTo(another.brandNm);
    }

}

}

所以如何解决相同的问题

1 个答案:

答案 0 :(得分:2)

您展示的是toString Object的实施情况。如果您不想拥有自定义适配器,可以覆盖toString()类中的Brand并让它返回您要显示的字符串,