【发布时间】:2014-05-02 22:35:11
【问题描述】:
我正在编写一个聊天应用程序。现在我正在尝试保存每个对话的消息。如果用户重新打开特定活动,我希望应用程序加载他已经发送的消息。
我现在的问题如下:
我将对话数据保存在这样的字符串中:
SEND#Hey youRECEIVE#嗨,你好吗?SEND#我很好,你呢?RECEIVE#=) 我也...
等等。你明白这个概念。
有什么方法可以在我称之为“分隔符”SEND# 和 RECEIVE# 之间挑选出消息片段,而不必使用大量的 if 案例?这是我现在的代码:
if(conversation != null){
while(conversation.contains("SEND123")|| conversation.contains("RECEIVE123")){
if(conversation.startsWith("SEND123")){
String rest = conversation.substring(7);
System.out.println("erste if: send: " + rest);
if(rest.contains("SEND123") && rest.contains("RECEIVE123")){
if(rest.indexOf("SEND123")<rest.indexOf("RECEIVE123")){
String messageReady = rest.substring(0, rest.indexOf("SEND123"));
conversation = rest.substring(rest.indexOf("SEND123"));
System.out.println(messageReady);
}
if(rest.indexOf("RECEIVE123")<rest.indexOf("SEND123")){
String messageReady = rest.substring(0, rest.indexOf("RECEIVE123"));
conversation = rest.substring(rest.indexOf("RECEIVE123"));
System.out.println(messageReady);
}
}else{
if(rest.contains("SEND123")){
String messageReady = rest.substring(0, rest.indexOf("SEND123"));
conversation = rest.substring(rest.indexOf("SEND123"));
System.out.println(messageReady);
}
if(rest.contains("RECEIVE123")){
String messageReady = rest.substring(0, rest.indexOf("RECEIVE123"));
conversation = rest.substring(rest.indexOf("RECEIVE123"));
System.out.println(messageReady);
System.out.println("if contains receive: " + conversation);
}
if(!rest.contains("SEND123") || !rest.contains("RECEIVE123")){
conversation = rest;
String messageReady = rest;
System.out.println(messageReady);
}
}
}
if(conversation.startsWith("RECEIVE123")){
String rest = conversation.substring(10);
System.out.println("erste if: receive: " + rest);
if(rest.contains("SEND123") && rest.contains("RECEIVE123")){
if(rest.indexOf("SEND123")<rest.indexOf("RECEIVE123")){
String messageReady = rest.substring(0, rest.indexOf("SEND123"));
conversation = rest.substring(rest.indexOf("SEND123"));
System.out.println(messageReady);
}
if(rest.indexOf("RECEIVE123")<rest.indexOf("SEND123")){
String messageReady = rest.substring(0, rest.indexOf("RECEIVE123"));
conversation = rest.substring(rest.indexOf("RECEIVE123"));
System.out.println(messageReady);
}
}else{
if(rest.contains("SEND123")){
String messageReady = rest.substring(0, rest.indexOf("SEND123"));
conversation = rest.substring(rest.indexOf("SEND123"));
System.out.println(messageReady);
}
if(rest.contains("RECEIVE123")){
String messageReady = rest.substring(0, rest.indexOf("RECEIVE123"));
conversation = rest.substring(rest.indexOf("RECEIVE123"));
System.out.println(messageReady);
}
if(!rest.contains("SEND123") || !rest.contains("RECEIVE123")){
conversation = rest;
String messageReady = rest;
System.out.println(messageReady);
}
}
}
}
}
我还没有完全完成它。我只是想向你展示它的样子。这必须花费大量的运行时间。有没有更简单的方法可以将用户收到的消息与他发送的消息分开,但保持正确的顺序?
也许我必须以不同的方式解决这个问题。也许将每个对话保存在一个字符串中并不是最好的主意。我打赌你可以帮助我或给我一些建议。
【问题讨论】:
-
按照你的方式做这件事比必须做的要困难得多。我建议使用 SQLite 数据库来保存信息。您可以找到一些一般信息here。
-
是否可以更改您的标志发送/接收?在那种情况下,它会容易得多。
-
我接受了我认为用户在对话期间很可能不会输入的标志。你会建议什么标志?
-
永远不要猜测用户不会做什么。他们会这样做的 ;-)
标签: java android string algorithm