Android .split无法正常工作

时间:2013-04-11 11:13:54

标签: java android

嗨,下面的代码似乎没有正常工作是否有什么我做错了?

基本上发生了什么,我收到了字符串prd|50126057|12bars|5,我试图把它分成三个不同的textViews;当我运行它时,它只在第一个textView中显示prd而其他的不会更改。为什么会这样?

我可以看到它在日志中工作,它只是没有在其他文本视图中显示它。

非常感谢任何帮助,并提前致谢。

另外我很抱歉,如果已经回复了我在这里做了一些搜索但找不到任何东西。

public void messageReceived(String message) {

    String response = message;
    String[] words = response.split("\\|");
    TextView tv1 = (TextView) findViewById(R.id.textView1);
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    TextView tv3 = (TextView) findViewById(R.id.textView3);

    tv1.setText(words[0]);
    tv2.setText(words[1]);
    tv3.setText(words[2]);
    Log.e("items-->", "" + words[0] + " " + words[1] + " " + words[2]+ " " + words[3]);

    publishProgress(message);
}

6 个答案:

答案 0 :(得分:1)

 String[] words;
 String s="prd|50126057|12bars|5"; 
 words= s.split("\\|");
 for(int i=0;i<words.length;i++)
 {
    System.out.println("Shifted: " +words[i]);
 }

答案 1 :(得分:1)

您在项目中使用了AsyncTask<Void, Void, Void> { ... }。如果您使用的是AsyncTask,则可以使用onProgressUpdate(Progress...)onPostExecute(Result)方法更新UI。您还调用了publishProgress(message)方法。因此,您可以使用onProgressUpdate(Progress...)onPostExecute(Result)方法更新UI(将字符串分配给Textview)。例如:

public class connectTask extends AsyncTask<String,String,SOCKETClient> {
    @Override
    protected SOCKETClient doInBackground(String... message) {
        socketClient = new SOCKETClient(new SOCKETClient.OnMessageReceived() {
            @Override
            public void messageReceived(String message) {
                // this method calls the onProgressUpdate
                publishProgress(message);
            }
        });
        socketClient.run();
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);

        String response = values[0];
        String[] words = response.split("\\|");
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        TextView tv2 = (TextView) findViewById(R.id.textView2);
        TextView tv3 = (TextView) findViewById(R.id.textView3);

        tv1.setText(words[0]);
        tv2.setText(words[1]);
        tv3.setText(words[2]);
    }
}

它应该有用。

答案 2 :(得分:0)

使用:

String response = "abc|asd|asd|asd";
String[] words = response.split("\\|");
Log.e("items-->", "" + words[0] + " " + words[1] + " " + words[2]+ " " + words[3]);

o / p:

04-11 16:55:58.216: E/items-->(3885): abc asd asd asd

Edited:

String response = "abc|asd|asd|asd";

    String[] words = response.split("\\|");

   // Log.e("items-->", "" + words[0] + " " + words[1] + " " + words[2]+ " " + words[3]);

   TextView tv = (TextView)findViewById(R.id.text);

   tv.setText("" + words[0] + " " + words[1] + " " + words[2]+ " " + words[3]);

上次编辑:

        String response = "abc|asd|asd|asd";

        String[] words = response.split("\\|");

        // Log.e("items-->", "" + words[0] + " " + words[1] + " " + words[2]+
        // " " + words[3]);

        TextView tv = (TextView) findViewById(R.id.textView1);
        TextView tv1 = (TextView) findViewById(R.id.textView2);
        TextView tv2 = (TextView) findViewById(R.id.textView3);
        TextView tv3 = (TextView) findViewById(R.id.textView4);

        tv.setText("" + words[0]);
        tv1.setText("" + words[1]);
        tv2.setText("" + words[2]);
        tv3.setText("" + words[3]);

enter image description here

答案 3 :(得分:0)

你的单词数组的大小是多少?

String test = "prd|50126057|12bars|5";
String [] words = test.split("\\|");
System.out.println(words.length);

为我提供了4个预期。

答案 4 :(得分:0)

代码看起来不错。要么没有被调用,要么传递错误的字符串。

我建议添加日志记录,看看会发生什么:

Log.d("~~~","@@@@ response=["+response+"]");

和其他人建议的那样,words.length和项目。

答案 5 :(得分:-1)

String response = "prd|50126057|12bars|5";
String[] words = response.split("|");
Toast.makeText(mActivity, words[0]+"::"+words[1]+"::"+words[2]+"::"+words[3],Toast.LENGTH_SHORT).show();

上面的代码没有任何错误。只需检查是否正在调用messageReceived。