-1

我正在尝试开发具有从数据库获取的用户列表的应用程序我在刷新列表视图时遇到问题:

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();
    }
}
4

1 回答 1

0

在您拥有的适配器上notifyDataSetChanged()

代替 :

SP.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));

你应该做

 SP.getAdapter().notifyDataSetChanged(); 

在每个添加的项目之后,或在所有项目都添加之后

于 2013-04-28T22:32:15.013 回答