开发者!
我正在开发Android APP,但我是Java新手,目前在android texView中显示来自SQLite数据库的十进制值有问题。
这是我的适配器代码段:
public Anuncio getAnuncio(int anuncioID) {
Anuncio Ad = new Anuncio();
String[] columns = new String[]{"id", "header", "body", "name", "phone", "email", "rating", "cat_id", "supercat_id", "price", "date", "image", "hits", "promo_type"};
Cursor cursor = this.db.query(false, "ads", columns, "id = " + anuncioID, null, null, null, null, "1");
cursor.moveToFirst();
Ad.setAd_id(cursor.getInt(cursor.getColumnIndex("id")));
Ad.setAd_title(cursor.getString(cursor.getColumnIndex("header")));
Ad.setAd_price(cursor.getDouble(cursor.getColumnIndex("price")));
Ad.setAd_image(cursor.getBlob(cursor.getColumnIndex("image")));
Ad.setAd_desc(cursor.getString(cursor.getColumnIndex("body")));
Ad.setAd_name(cursor.getString(cursor.getColumnIndex("name")));
Ad.setAd_phone(cursor.getString(cursor.getColumnIndex("phone")));
Ad.setAd_email(cursor.getString(cursor.getColumnIndex("email")));
Ad.setAd_category(cursor.getInt(cursor.getColumnIndex("cat_id")));
Ad.setAd_supercategory(cursor.getInt(cursor.getColumnIndex("supercat_id")));
Ad.setAd_date(cursor.getString(cursor.getColumnIndex("date")));
Ad.setAd_hits(cursor.getInt(cursor.getColumnIndex("hits")));
Ad.setAd_promo_type(cursor.getInt(cursor.getColumnIndex("promo_type")));
cursor.close();
return Ad;
}
我想要的字段是“价格”字段,这是一种REAL数据类型。 在我的TextView中只显示一个小数为.0的数字,但有时价格可能是.32或其他小数值。
Anuncio课程:
public class Anuncio {
private int ad_id;
private String ad_title;
private String ad_desc;
private Double ad_price;
private String ad_name;
private String ad_email;
private String ad_phone;
private Float ad_rating;
private byte[] ad_image;
private int ad_category;
private int ad_supercategory;
private String ad_date;
private int ad_hits;
private int ad_promo_type;
public Anuncio() {
}
public int getAd_id() {
return ad_id;
}
public void setAd_id(int ad_id) {
this.ad_id = ad_id;
}
public String getAd_title() {
return ad_title;
}
public void setAd_title(String ad_title) {
this.ad_title = ad_title;
}
public String getAd_desc() {
return ad_desc;
}
public void setAd_desc(String ad_desc) {
this.ad_desc = ad_desc;
}
public Double getAd_price() {
return ad_price;
}
public void setAd_price(Double ad_price) {
this.ad_price = ad_price;
}
public String getAd_name() {
return ad_name;
}
public void setAd_name(String ad_name) {
this.ad_name = ad_name;
}
public String getAd_email() {
return ad_email;
}
public void setAd_email(String ad_email) {
this.ad_email = ad_email;
}
public String getAd_phone() {
return ad_phone;
}
public void setAd_phone(String ad_phone) {
this.ad_phone = ad_phone;
}
public Float getAd_rating() {
return ad_rating;
}
public void setAd_rating(Float ad_rating) {
this.ad_rating = ad_rating;
}
//Get only the first 160 chars
public String getAd_title_reduced() {
if (ad_title.length() > 140) {
return ad_title.substring(0, 139);
}
return ad_title;
}
//Get Imagen byte[]
public byte[] getAd_image() {
return this.ad_image;
}
//Set Imagen data
public void setAd_image(byte[] imagen) {
this.ad_image = imagen;
}
//Get Description reduced
public String getAd_desc_reduced() {
if (ad_desc.length() > 140) {
return ad_desc.substring(0, 139);
}
return ad_desc;
}
public int getAd_category() {
return ad_category;
}
public void setAd_category(int ad_category) {
this.ad_category = ad_category;
}
public int getAd_supercategory() {
return ad_supercategory;
}
public void setAd_supercategory(int ad_supercategory) {
this.ad_supercategory = ad_supercategory;
}
public String getAd_date() {
return ad_date;
}
public void setAd_date(String ad_date) {
this.ad_date = ad_date;
}
public int getAd_hits() {
return ad_hits;
}
public void setAd_hits(int ad_hits) {
this.ad_hits = ad_hits;
}
public int getAd_promo_type() {
return ad_promo_type;
}
public void setAd_promo_type(int ad_promo_type) {
this.ad_promo_type = ad_promo_type;
}
}
我的布局:
<TextView
android:id="@+id/ad_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:text="@string/nominal_price"
android:textColor="#4caf50"
android:textSize="15sp"
android:textStyle="bold" />
我的Anuncio类逻辑:
//Setting text in the layout
ad_title.setText(Ad.getAd_title_reduced());
if (Ad.getAd_price() > 0) {
ad_price.setText("$ " + Ad.getAd_price().toString());
} else {
ad_price.setText("");
}
答案 0 :(得分:1)
尝试删除toString()
ad_price.setText("$ " + Ad.getAd_price());
答案 1 :(得分:0)
试试这个
ad_title.setText(Ad.getAd_title_reduced());
if (Ad.getAd_price() > 0) {
double price = Double.parseDouble(Ad.getAd_price().toString());
ad_price.setText("$ " + new DecimalFormat("##.##").format(price));
} else {
ad_price.setText("");
}