2016-02-29 66 views
0

这是我解析Java代码的响应数据数据 -不能设置是走出从服务器到CustomListView适配器

pDialog = new ProgressDialog(getActivity()); 
//   Showing progress dialog before making http request 
     pDialog.setMessage("Loading...Please Wait..."); 
     pDialog.show(); 

     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://sikkimexpress.itstunner.com/api/homenewslist/topnews", new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 
        JSONArray jsonArray = response.getJSONArray("HomeNews"); 

        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject homenews = jsonArray.getJSONObject(i); 
         Movie movie = new Movie(); 
         String newsId = homenews.getString("NewsId"); 
         String dateTime = homenews.getString("DateTime"); 
         String newsType = homenews.getString("NewsType"); 
         String title = homenews.getString("Title"); 
         String description = homenews.getString("Description"); 
         String mainImageURL = homenews.getString("MainImageThumbnail"); 

         movieList.add(movie); 
         listView.setAdapter(adapter); 
         adapter.notifyDataSetChanged(); 
         System.out.println("Result:- " + newsId + " " + dateTime + " " + newsType + " " + title + " " + description + " " + mainImageURL); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
//    pDialog.hide(); 

      } 
     }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Log.e("VOLLEY", error.getMessage()); 
//      pDialog.hide(); 
        } 
     }); 

     AppController.getInstance().addToRequestQueue(jsonObjectRequest); 

这是模型类: -

public class Movie { 
    private String newsId; 
    private String dateTime; 
    private String newsType; 
    private String title; 
    private String description; 
    private String thumbnailUrl; 

    public Movie() { 
    } 

    public Movie(String news_id, String date_time, String news_type, String news_title, String news_description, String news_thumbnailUrl) { 
     this.title = news_title; 
     this.thumbnailUrl = news_thumbnailUrl; 
     this.newsId = news_id; 
     this.dateTime = date_time; 
     this.newsType = news_type; 
     this.description = news_description; 
    } 

    public String getNewsId() { 
     return newsId; 
    } 

    public void setNewsId(String newsId) { 
     this.newsId = newsId; 
    } 

    public String getDateTime() { 
     return dateTime; 
    } 

    public void setDateTime(String dateTime) { 
     this.dateTime = dateTime; 
    } 

    public String getNewsType() { 
     return newsType; 
    } 

    public void setNewsType(String newsType) { 
     this.newsType = newsType; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getThumbnailUrl() { 
     return thumbnailUrl; 
    } 

    public void setThumbnailUrl(String thumbnailUrl) { 
     this.thumbnailUrl = thumbnailUrl; 
    } 
} 

的CustomListView适配器: -

public class CustomListAdapter extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<Movie> movieItems; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

    public CustomListAdapter(Activity activity, List<Movie> movieItems) { 
     this.activity = activity; 
     this.movieItems = movieItems; 
    } 

    @Override 
    public int getCount() { 
     return movieItems.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return movieItems.get(location); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.list_row, null); 

     if (imageLoader == null) 
      imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail); 
     TextView title = (TextView) convertView.findViewById(R.id.title); 
     TextView desciption = (TextView) convertView.findViewById(R.id.desciption); 

     Movie m = movieItems.get(position); 
     thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader); 
     title.setText(m.getTitle()); 
     desciption.setText(m.getDescription()); 

     return convertView; 
    } 

} 

没有错误,同时从服务器解析的数据。我正在得到实际结果。但进度对话框在从服务器获取数据后运行。数据没有在CustomListView适配器中设置。我已经附上了代码。请帮帮我。我陷入了困境。

回答

1

当你有你的数据时,你没有关闭Dialog

您不应该在“主线程”中加载数据 - 使用AsyncTask或类似的东西来加载您的数据。在那里,你可以显示一个进度对话框,你开始下载数据之前:

docs

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 

//Once created, a task is executed very simply: 

new DownloadFilesTask().execute(url1, url2, url3); 

此外,不要多次适配器设置为您ListView(除非你使用一个不同的适配器),每当您的基础数据发生变化时请致电notifyDataSetChanged()。 r数据,显示进度并在完成后停止对话。

+0

我正在使用凌空。该怎么做?请帮忙。 – user312456

0

但是进度对话框在从服务器获取数据后运行。

答:你是不是关闭该对话框中onResponse方法

对于列表视图你是不是有更新的数据设置适配器。请创建一个新的适配器或按照这个How to update listview when back pressed from another activity android?

+0

我正在使用凌空。该怎么做?请帮忙。 – user312456

+0

在您的代码中有可能绑定数据到列表视图 –

0

您需要关闭这两个响应方法中的progressdialog才能隐藏。

@Override public void onResponse(JSONObject response){pDialog.dismiss(); }

@Override public void onErrorResponse(VolleyError error){pDialog.dismiss(); }

在解析json之后的onResponse()方法中,您需要通知适配器在列表中显示数据。