【问题标题】:Android / Java Algorithm to pick out pieces of StringAndroid / Java算法挑选出字符串
【发布时间】: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


【解决方案1】:

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

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

您可能还想要:

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

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

编辑:

这是official link to using SQLlite on Android ,这是fairly complete tutorial

【讨论】:

  • 我不太明白 sqlite 是如何工作的。然后我可以保存“消息实例”吗?
  • 使用 SQLite 有一个学习曲线,但是学习它所花费的时间将少于实现自己的存储协议所花费的时间。
  • 但我想我会努力做到这一点。看起来它有很多优点。例如,节省特定时间是我刚刚想到的另一个方法
  • 我同意使用数据库来存储您的消息。它将更快/更容易/提供宝贵的学习经验!我说把这个用户说的话混在一起,有一个message_to和一个message_from
【解决方案2】:

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

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]...

现在您可以将数组附加到包含对话的文本字段中。

【讨论】:

  • 问题在于对话中没有发送接收发送接收的逻辑重复。可能是一个用户一直在说话,而另一个用户很少说话
  • 确实如此。一个快速解决方法是仅在最后一个说话的人不同时才附加分隔符。尽管您可能会更好地更改保存聊天记录的结构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 2014-09-21
相关资源
最近更新 更多