gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:19:35 25 4
gpt4 key购买 nike

这是我正在解析数据的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com