Android - 使用HashMap获取单个String值

时间:2014-06-03 10:59:16

标签: android hashmap

在我的应用程序中,我使用ArrayList预装了Spinner.ArrayList包含多个Text String。这些文本字符串将作为消息template用户可以从微调器中选择。此外,我希望在发送消息时,这些String关键字可能会被变量替换。就像“Text”将替换为EditText内容一样,“Phone No”替换为Sender的no,“Date”替换为Message Date

我试图在Android中搜索HashMap,但遇到问题:

HashMap<String, String> template=new HashMap<String, String>();
 template.put("Text", editText.getText().toString());
 template.put("Phone No",senderPhone);
 template.put("Date",receivedDate);

现在我想将它显示为单字符串,如文本,电话号码,日期。这个字符串是否可以编辑。

2 个答案:

答案 0 :(得分:0)

要从Hashmap获取值,请使用:

String Text   = (String)template.get("Text");
String phoneNo= (String)template.get("PhoneNo");
String date   = (String)template.get("Date");

您可以将以上所有3组合在一起,并将其显示在您想要的位置 例如,

String displayString = Text + "-" + phoneNo + " -" + date
editText.setText(displayString);

默认情况下,此编辑文字可以编辑,除非您将其设为不可编辑。!

编辑:(为了编辑而引用评论)

(1)将编辑文本值设置为用户从微调器中选择的值。

editText.setText(displayString);

(2)用户选择并修改edittext后,让用户点击确认按钮。

(3)在确认按钮代码中,将值添加到本地存储( IF REQUIRED!)。

(4)刷新微调器以包含此修改值

要刷新,如果要修改微调器中的现有列表,请参阅Refresh spinner data

要刷新,如果要保留现有的微调器列表,并添加新值,请参阅dynamic add data to spinner but not update the data on the spinner

答案 1 :(得分:0)

您可以使用

StringBuilder builder = new StringBuilder();

for (String name : template.values())
{
    builder.append(name + " ");
}

String templateString = builder.toString();

这将使用HashMap获取values()中的所有值,并将其连接到String