Android / Java算法挑选出一些String

时间:2014-05-02 22:35:12

标签: java android string algorithm

我正在写一个聊天应用程序。现在我正在尝试保存每个对话的消息。如果用户重新打开特定活动,我希望应用程序加载他已发送的消息。

我现在的问题是:

我将会话数据保存在这样的字符串中:

  

发送#嘿youRECEIVE#你好吗?发送#我很好你呢?收到#=)所以我......

等等。你得到了这个概念。

有没有办法在我所谓的“分隔符”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);
                        }

                    }


                    }
                }
            }

我还没完成它。我只想告诉你它的样子。这必须占用大量的运行时间。有没有更简单的方法来分隔用户从他发送的邮件中获取的邮件,但是保持它们的顺序正确?

也许我必须以不同的方式解决这个问题。也许将每个会话保存在字符串中并不是最好的主意。我打赌你可以帮助我或给我一些提示。

2 个答案:

答案 0 :(得分:0)

使用小型SQLite数据库将更容易管理。您可以拥有以下列:

  • sequence_num,一个唯一标识每条消息的自动递增整数
  • remote_id,远程合作伙伴的标识符
  • direction,一个表示发送与接收的布尔标志
  • message,消息文本

你也可能想要:

  • timestmp,发送/接收邮件的时间
  • conversation_id,一个自动递增的整数,用于唯一标识每个对话(由您定义)

以及您希望跟踪消息的任何其他数据。您可能还想添加其他表来保留每个remote_id等的关联连接数据。

编辑:

此处为official link to using SQLlite on Android ,此处为fairly complete tutorial

答案 1 :(得分:0)

如果您不想像其他建议那样使用数据库,那么这个云就是一个解决方案。

String history = #SEPARATORHey you#SEPARATORHi how are you?#SEPARATORI´m fine and you?#SEPARATORGood thx#SEPARATOR;

If you do this you could use split like this:

String[] arrayHistory = history.split("#SEPARATOR"); // array now looks like this: [Hey you][Hi how are you?][I'm fine and you?][Good thx]...[send][rec][send][rec]...

现在,您只需将对话附加到文本字段即可。