java - 无法设置作为从服务器到 CustomListView 适配器的响应而出现的数据

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

}

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

最佳答案

当您拥有数据时,您不会关闭对话框

您不应该在“主线程”上加载数据 - 使用 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 数据,显示进度并在完成后停止对话框。

关于java - 无法设置作为从服务器到 CustomListView 适配器的响应而出现的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35694121/

相关文章:

javascript - 有没有办法用 XMLHttprequest 调用多个 URL?

java - Java是否能够进行进程监控?

java - 所有按钮都从 Android fragment 打开相同的 Activity

java - 使用 genson 将 json 字符串反序列化为流体对象

android - Visual Studio 2015 上的 Xamarin - 在指定的 SDK 路径中找不到 adb.exe

Android BLE 连接断开

json - Bash 脚本使用 jq 遍历多行 JSON 对象

java - 运行 R scraper 脚本时出现 java.io.IOException 错误

java - 如何显示和隐藏现有的 JFrame

android - 应用程序进入后台时不显示应用程序图标