【问题标题】:android ArrayList to clean text to share by emailandroid ArrayList 清理文本以通过电子邮件共享
【发布时间】:2016-02-17 20:26:13
【问题描述】:

我正在使用下面的代码从我的SQLite db 行中检索两个参数,TITLENOTE。我在allnotesArrayList 中列出了所有内容。 我现在要做的是通过电子邮件以干净的格式分享ArrayList

DatabaseConnector dbConnector = new DatabaseConnector(MainActivity.this);
        dbConnector.open();
        c = dbConnector.ListAllNotesFull();
        while(c.moveToNext()) {
            Map<String, String> data = new HashMap<String, String>(2);
            data.put(TITLE, c.getString(c.getColumnIndex(TITLE)));
            data.put(NOTE, c.getString(c.getColumnIndex(NOTE)));
            allnotes.add(data);
        }
        dbConnector.close();

        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yy");
        String formattedDate = df.format(c.getTime());

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
        i.putExtra(Intent.EXTRA_SUBJECT, "My app: my workouts backup " + formattedDate);
        i.putExtra(Intent.EXTRA_TEXT, allnotes.toString());
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

但我在邮件正文中得到的结果是:

[{note=blabla, title=test}, {note=gdgfg, title=test2}]

我应该改用数组吗?无论如何,我怎样才能将它格式化为一个干净的字符串?谢谢


编辑:我希望列表的格式很简单

标题

注意

(这里有空格)

标题

注意

等等。 (不需要加粗)

【问题讨论】:

  • 在这里使用数组对您没有帮助。您需要按照您希望的方式手动对其进行格式化。请使用您想要的格式更新您的帖子。
  • 嗨,感谢您的宝贵时间,现在编辑

标签: android arraylist format tostring


【解决方案1】:

您需要遍历 hashmap 并获取 Notes、Titles 值并将其附加到字符串中。 您可能对这样的事情感兴趣:

String noteTitleList = "";
    for(Map<String, String> data: allnotes)
    {
        Iterator it = data.entrySet().iterator();
        while (it.hasNext()) 
        {
            Map.Entry pair = (Map.Entry)it.next();
            noteTitleList+= pair.getKey()+": "+pair.getValue()+"\n";
        }
        noteTitleList+= "\n";
    }

在此 for 循环之后,您将获得所需格式的 NoteTitle 对列表。您可以在此之后调用 Intent。

编辑:为了适应您的格式,如下所示,我修改了上面的代码。

标题

注意

(这里有空格)

标题

注意

【讨论】:

  • 感谢您的回答,我得到的代码是(使用正确的垂直空格):Title: note Note: blabla Title: title Note: test Title: note Note: gdgfg Title: title Note: test2
  • 在注释之前列出标题足以反转我的哈希映射代码中的data.put,但我仍然得到所有这些重复,如上面的回复所示
  • 将代码的最后一行更改为 noteTitleList+= pair.getValue()+"\n"; 列出了一切正常,但我错过了对之间的空格
  • 感谢您澄清要求。我再次编辑了它。这应该会有所帮助。
  • 它解决了,谢谢!我刚刚摆脱了pair.getKey()+": "+,因为我不希望“标题”和“注释”这两个词出现在各自的内容之前,但本质上是相同的。欢呼
猜你喜欢
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
相关资源
最近更新 更多