我正在尝试开发一个应用程序,其中包含从我有问题的数据库到刷新列表视图的用户列表:
void list(){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://*******/login/test.php");
List<NameValuePair> nameValue = new ArrayList<NameValuePair>();
httppost.setEntity(new UrlEncodedFormEntity(nameValue));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//jonson
String username;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
username=json_data.getString("username");
items.add(username);
SP.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
答案 0 :(得分:0)
在适配器上有notifyDataSetChanged()
而不是:
SP.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
你应该做
SP.getAdapter().notifyDataSetChanged();
在每个添加的项目之后,或者在添加所有项目之后