首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ParseQuery:库中价目表转换为字符串android

ParseQuery:库中价目表转换为字符串android
EN

Stack Overflow用户
提问于 2015-12-06 14:54:43
回答 2查看 111关注 0票数 0

​大家好!

我试图建立一个滚动的自定义列表视图,显示按价格上升排序的产品列表。然而,我刚刚意识到我将价格存储为字符串,这意味着$1000.00在$2.01之前,因为它是一个字符而不是数字。我已经在Parse上将我的数据转换为一个“数字”,并相信最好的检索类型是一个双精度型(有人能对此发表评论吗?)问题是,我需要将它保存为数字,将其转换为字符串,然后将其传递给listview,以便在文本字段中显示。最初我有

代码语言:javascript
复制
PPI.setProductprice((String) product.get("Price")); 

如下所示:

代码语言:javascript
复制
            // Locate the class table named "Products" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "Products");
            // Locate the column named "Price" in Parse.com and order list
            // by ascending
            query.orderByAscending("Price");
            ob = query.find();
            for (ParseObject product : ob) {
                // Locate images in PrimaryPhoto column
                ParseFile productimage = (ParseFile) product.get("PrimaryPhoto");

                ProductPopulation PPI = new ProductPopulation();

                PPI.setProductname((String) product.get("Name"));
                PPI.setProductbrand((String) product.get("Brand"));
                PPI.setProductprice((String) product.get("Price"));
                PPI.setProductimage(productimage.getUrl());
                productpopulationlist.add(PPI);

然后,我尝试将其放入一个双精度数组中,并对其进行迭代以将其转换为字符串。

我的上一次尝试可能没有意义,是这样更改的:

代码语言:javascript
复制
PPI.setProductprice((Double) product.getDouble("Price"));

我是相当了解安卓和任何你能给我的帮助将不胜感激。

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2015-12-06 15:58:44

好的,这里我没有得到上下文,但是你可以做的是将价格保存为一个字符串,在提取它的时候,你可以在字符串上调用Integer.parseInt(String intvalue);,将值转换回int,然后你就可以做所有你应该做的操作了。您可以从服务器获取一个无序数组,并将其安排在设备级别,这将为您节省一些时间和逻辑。

票数 0
EN

Stack Overflow用户

发布于 2015-12-06 16:24:48

我不知道您用来填充列表的ProductPopulation类是什么,所以我不能说在您的情况下确切地说什么是最好的方法,但是通常可以通过Collections.sort()方法(请参见the method documentation)对列表进行排序。

您可以在将列表添加到列表视图之前对列表进行排序。要以所需的方式对列表进行排序,必须提供一个比较器,然后从组成列表的对象中获取所需的值(字段或方法结果),比较所获得的值,并根据比较结果返回-1、0或1。它可能看起来有点像这样:

代码语言:javascript
复制
for (...) {
  ItemClass newObject = new ItemClass(); // new list item
  // ...here add the values to the list item...
  theList.add(newObject); // add the new item to the list 
}

// now sort the list before adding it to the list viewer 
Collections.sort(theList, new Comparator<ItemClass>() {
  @Override
  public int compare(ItemClass o1, ItemClass o2) {
     // obtain and compare the values you need 
    return Double.compare(o1.getDouble(), o1.getDouble());
       // you could also do something like 
       //  Double.compare(
       //     Double.parseDouble(o1.getString()),   
       //     Double.parseDouble(o2.getString()));
       // but it would be much slower   
  }
});

// now add the sorted list to the viewer 
listViewer.setList(theList);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34114899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档