无法将作为服务器响应的数据设置为CustomListView适配器

时间:2016-02-29 07:33:50

标签: java android json listview

这是我解析数据的Java代码 -

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;
    }

}

从服务器解析数据时没有错误。我得到了实际的结果。但是从服务器获取数据后,Progress Dialog正在运行。未在CustomListView适配器中设置数据。我已经附上了代码。请帮我。我陷入其中。

3 个答案:

答案 0 :(得分: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数据,显示进度并在完成后停止对话框。

答案 1 :(得分:0)

  

但是从服务器获取数据后,Progress Dialog正在运行。

ans:您没有在onResponse方法

中关闭对话框

对于listview,您没有使用更新的数据设置适配器。请创建新的适配器或遵循此How to update listview when back pressed from another activity android?

答案 2 :(得分:0)

你需要在这两个响应方法中解除progressdialog以隐藏。

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

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

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