【问题标题】:android- convert arraylist to one string in custom formatandroid-将arraylist转换为自定义格式的一个字符串
【发布时间】:2018-02-27 18:14:00
【问题描述】:

我在 sqlit 数据库中有数据,所以我用它来存储类别和项目 我像这样在arraylist中得到它:

    public ArrayList showDataItems(String id_cate){
    ArrayList<Items>  arrayListItems = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cr  = db.rawQuery("select * from items where id_cate = "+id_cate,null);

    cr.moveToFirst();

    while (cr.isAfterLast() == false){
        String item_id = cr.getString(0);
        String ItemName = cr.getString(1);
        String Item_quantity = cr.getString(2);
        String icon = cr.getString(3);
        int isDone  = Integer.parseInt(cr.getString(5));

        arrayListItems.add(new Items(item_id,ItemName,Item_quantity,R.drawable.shopicon,icon,isDone));
        cr.moveToNext();
    }
    return  arrayListItems;
}

所以我需要获取这些数据并将其转换为字符串并以自定义格式将其共享给其他应用程序,例如 whatsapp:

1- 第一个 *
2- 第二个 *
3-....

所以我使用此代码发送数据

     Intent intent = new Intent(Intent.ACTION_SEND);
         intent.putExtra(Intent.EXTRA_TEXT,"hello world");
        intent.setType("text/plain");
        startActivity(Intent.createChooser(intent,"send items i need all 
        data here"));

所以我们可以使用字符串生成器或其他东西来获取一个字符串中的数据 请帮帮我!

【问题讨论】:

  • 你想发送什么数据到whatsapp?

标签: java android string arraylist


【解决方案1】:

正如你所说,有一个 StringBuilder 类可以帮助格式化字符串。 Here are the java docs

在 StringBuilder 中,查看 append(..) 方法。以您为例:

    int size = list.size();
    for(int i = 0; i< size-1;i++){
        stringBuilder.append(i+1).append("- ").append(list.get(i)).append('\n');
    }
    stringBuilder.append(size).append("- ").append(list.get(size-1));

最后一次调用 append 与第一次调用不同,不附加结束行

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 2017-02-23
    • 2023-03-11
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多