如何在String中存储StringBuilder值

时间:2015-08-19 09:43:50

标签: java android

我想在String变量中获取StringBuilder的值:

        String strName = detailArrayListFromDB.get(i).getName();
        int dbQty = detailArrayListFromDB.get(i).getQuantity();
        double dbTotal = detailArrayListFromDB.get(i).getTotal();

        Log.d("Name::--", strName);
        Log.d("QTY::--", ""+dbQty);
        Log.d("TOTAL::--", ""+dbTotal);

        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("Name: "+strName);
        strBuilder.append("Qty:" +dbQty);
        strBuilder.append("Total:" +dbTotal);
        String str = strBuilder.toString();
        Log.d("stringBuilder:", str);

Log显示:

D/Name::--(20881): Mobile
D/QTY::--(20881): 1
D/TOTAL::--(20881): 20000.0
D/stringBuilder:(20881): Name: MobileQty:1Total:20000.0

D/Name::--(20881): Laptop
D/QTY::--(20881): 1
D/TOTAL::--(20881): 30000.0
D/stringBuilder:(20881): Name: LaptopQty:1Total:30000.0

这就是我要想得到输出

D/stringBuilder:(20881): Name: Mobile Qty:1 Total:20000.0, Name: Laptop Qty:1 Total:30000.0

2 个答案:

答案 0 :(得分:2)

要输入循环,请执行以下操作:

       StringBuilder strBuilder = new StringBuilder();
      for(int i = 0; i < <waht u need>; i++){
            String strName = detailArrayListFromDB.get(i).getName();
            int dbQty = detailArrayListFromDB.get(i).getQuantity();
            double dbTotal = detailArrayListFromDB.get(i).getTotal();

            Log.d("Name::--", strName);
            Log.d("QTY::--", ""+dbQty);
            Log.d("TOTAL::--", ""+dbTotal);


            strBuilder.append(" Name: "+strName);
            strBuilder.append(" Qty:" +dbQty);
            strBuilder.append(" Total:" +dbTotal);
    }
    String str = strBuilder.toString();
    Log.d("stringBuilder:", str);

答案 1 :(得分:1)

StringBuilder只是将字符串连接在一起的一种方法。它不会添加你自己必须做的格式。例如:

Unable to auto detect APP_BUNDLE_PATH.