android ArrayList 清理文本以通过电子邮件共享

android ArrayList to clean text to share by email

我正在使用下面的代码从我的 SQLite 数据库行中检索两个参数,TITLENOTE。我在 allnotes ArrayList 中列出所有内容。 我现在要做的是以干净的格式通过电子邮件分享 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();
        }

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

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

我应该改用数组吗?我怎样才能将它格式化为干净的字符串呢?谢谢


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

标题

备注

(此处为space)

标题

备注

等 (不需要加粗)

您需要遍历 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了。

编辑:为了适应下面给出的格式,我修改了上面的代码。

标题

备注

(此处为space)

标题

备注