[英]Android .split not working correctly
嗨,下面的代碼似乎無法正常工作,我在做錯什么嗎?
基本上發生了什么,我收到了字符串prd|50126057|12bars|5
,我試圖將其拆分為三個不同的textViews。 當我運行它時,它只在第一個textView中顯示prd,其他的都沒有改變。 為什么會這樣?
我可以看到它在日志中正常工作,只是在其他textview中沒有顯示它。
非常感謝您的任何幫助,並在此先感謝。
另外,如果對此已經得到回答,我很抱歉,我確實在這里做了一些搜索,但找不到任何東西。
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);
}
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]);
}
您在項目中使用了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]);
}
}
它應該工作。
采用:
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]);
您的單詞數組的大小是多少?
String test = "prd|50126057|12bars|5";
String [] words = test.split("\\|");
System.out.println(words.length);
給出4對我的期望。
該代碼看起來還可以。 要么不調用它,要么將錯誤的字符串傳遞給它。
我建議添加日志記錄,看看會發生什么:
Log.d("~~~","@@@@ response=["+response+"]");
以及其他建議, words.length
, words.length
和項目。
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是否被調用即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.