我有一个AutoCompleteTextView,它使用动态适配器。从下拉列表中选择项目时,将引发StringIndexOutOfBoundsException。
异常似乎发生在取决于添加到适配器的String的内容。 这是我发现的:
这是我的AsyncTask的onPostExecute代码:
protected void onPostExecute(String result) {
Log.i("","onPostExecute");
ArrayList<String> test = new ArrayList<String>();
test.add(new String("Microsoft"));
test.add(new String("Tesla"));
test.add(new String("Google"));
String str_JSON = result.substring(result.indexOf("(") + 1, result.lastIndexOf(")"));
Log.i("", "JSON obtained: " + str_JSON);
try {
JSONObject obj_JSON = new JSONObject(str_JSON);
JSONArray arr_JSON = obj_JSON.getJSONObject("ResultSet").getJSONArray("Result");
Log.i("", "arr_JSON Length: " + arr_JSON.length());
for(int index = 0; index < arr_JSON.length(); index++) {
JSONObject row_JSON = arr_JSON.getJSONObject(index);
String str_autocomplete = row_JSON.getString("symbol")
+ ", " + row_JSON.getString("name")
+ " (" + row_JSON.getString("exch") + ")";
test.add(new String(str_autocomplete));
}
Log.i("", "list_autocomplete Length: " + COMPANIES.size());
} catch (Exception e) {};
adapter.clear();
adapter.addAll(test);
Log.i("", "adapter Count: " + adapter.getCount());
AutoCompleteTextView t = (AutoCompleteTextView)findViewById(R.id.text_company);
t.setAdapter(adapter);
}
那么,如何构建String以便AutoCompleteTextView不会破坏?
答案 0 :(得分:0)
抱歉,由于声誉不佳,我无法撰写评论,所以我写这个作为答案......为什么要写test.add(new String(str_autocomplete));
而不仅仅是test.add(str_autocomplete);
?