通过昏迷为每个项目分隔对象数据,但不是最后一个

时间:2017-08-04 05:39:20

标签: java android string

想要一个字符串,用于以昏迷分隔的对象中的项目,而不是最后一项

 next_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < productList.size(); i++) {
                if (productList.get(i).isProductChecked) {

                    if (!multiId.contains(i))
                        multiId.add(i);
                    Product obj1 = productList.get(i);
                    String fname = obj1.getdescription();

                    List<String> productDescription newArrayList<String>();
                    productDescription.add(fname);


                    String join = productDescription.toString();

                        Log.e("SPLIT", join);

                } else {
                    if (multiId.contains(i))
                        multiId.remove(multiId.indexOf(i));
                }
            }

当前输出

08-04 11:50:37.273 2114-2114 / com.weenggs.flora E / SPLIT:[新产品2]

08-04 11:50:37.273 2114-2114 / com.weenggs.flora E / SPLIT:[新产品3]

期望输出

新产品2,新产品3

2 个答案:

答案 0 :(得分:0)

你在这里:

String object = "apple,orange,banana"; //as you said, you should get apple and orange

object = object.substring(0, object.lastIndexOf(","));
String[] result = object.split(",");

结果是数组包含除最后一项之外的对象中的所有项目!

答案 1 :(得分:0)

答案

1.声明一个数组列表。

List<String> productPrice = new ArrayList<String>();

2.在该数组列表中存储一个字符串对象

fname = obj1.getPrice();
 productPrice.add(fname);

3.最后使用text utils通过逗号连接该数组列表项。

android.text.TextUtils.join(",", productPrice);