带有RESTAPI响应的Android空ViewPager更新
我使用的是android ViewPager 我在开始时将viewpager设置为空列表,因为我只想显示从RESTAPI响应中获取的动态数据 因此,在给出适配器的空列表时。开头将有0页。除此之外,我正在运行AsynTask,它将从服务器获取JSON格式的数据 我想用json响应的数组大小设置空视图分页器计数。 所以总页数=json数组的大小 但是当我尝试更新适配器时。它没有任何作用。保持只显示空屏幕 我在更新视图寻呼机时遇到问题 AsynTask.java的onPostExecute方法带有RESTAPI响应的Android空ViewPager更新,android,json,rest,android-fragments,android-viewpager,Android,Json,Rest,Android Fragments,Android Viewpager,我使用的是android ViewPager 我在开始时将viewpager设置为空列表,因为我只想显示从RESTAPI响应中获取的动态数据 因此,在给出适配器的空列表时。开头将有0页。除此之外,我正在运行AsynTask,它将从服务器获取JSON格式的数据 我想用json响应的数组大小设置空视图分页器计数。 所以总页数=json数组的大小 但是当我尝试更新适配器时。它没有任何作用。保持只显示空屏幕 我在更新视图寻呼机时遇到问题 AsynTask.java的onPostExecute方法 pro
protected void onPostExecute(String result) {
Log.d("resultOnPostExecute", result);
//JSONObject data;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
////Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
try {
if(!result.isEmpty()) {
JSONObject jObject = new JSONObject(result);
//data = jObject;
Log.d("STATUS", jObject.getString("success"));
String status = jObject.getString("success").toString();
if(status.equals("1")) {
JSONArray array = jObject.getJSONArray("reports");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
Log.d("id", jo.getString("id"));
Log.d("image", jo.getString("image"));
images.add(jo.getString("image"));
//adapter().notifyDataSetChanged();
/*HashMap<String, String> department = new HashMap<>();
department.put("id", jo.getString("id"));
department.put("name", jo.getString("name"));
list.add(department);*/
}
adapter.updateList(images);
////intent.putExtra("result", result);
}
else
Toast.makeText(getApplicationContext(), "No doctor found", Toast.LENGTH_LONG).show();
}
//// else
//// intent.putExtra("result", "NOTHONG FIND");
} catch (JSONException e) {
e.printStackTrace();
}
//// startActivity(intent);
}
package android.hmkcode.com.apicall;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
/**
* Created by umar on 3/7/2016.
*/
public class ViewPagerAdapter extends PagerAdapter {
Context c;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
public ViewPagerAdapter(Context c, ArrayList<String> imagePaths) {
Log.d("CUEW OAFE ADAORER", "constructor");
this._imagePaths = imagePaths;
Log.d("size",this._imagePaths.size()+"");
this.c = c;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.pager_item, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);
Picasso.with(c).load(_imagePaths.get(position)).into(imgDisplay);
(container).addView(viewLayout);
return viewLayout;
}
@Override
public void notifyDataSetChanged() {
Log.d("NOTFIY STATE CHANGED","NOTIFY STATE CHANGE");
super.notifyDataSetChanged();
}
@Override
public Parcelable saveState() {
return null;
}
/*public int getItemPosition(Object object) {
return POSITION_NONE;
}*/
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
(container).removeView((RelativeLayout) object);
}
public void updateList(ArrayList<String> images){
Log.d("updated list", "list update");
this._imagePaths = images;
Log.d("size",this._imagePaths.size()+"");
this.notifyDataSetChanged();
}
}
受保护的void onPostExecute(字符串结果){
Log.d(“resultOnPostExecute”,结果);
//JSONObject数据;
ArrayList=新建ArrayList();
////Intent Intent=newintent(getApplicationContext(),ResultActivity.class);
试一试{
如果(!result.isEmpty()){
JSONObject jObject=新JSONObject(结果);
//数据=作业对象;
Log.d(“STATUS”,jObject.getString(“success”);
字符串状态=jObject.getString(“成功”).toString();
如果(状态等于(“1”)){
JSONArray数组=jObject.getJSONArray(“报告”);
对于(int i=0;i
ViewPagerAdapter.java
protected void onPostExecute(String result) {
Log.d("resultOnPostExecute", result);
//JSONObject data;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
////Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
try {
if(!result.isEmpty()) {
JSONObject jObject = new JSONObject(result);
//data = jObject;
Log.d("STATUS", jObject.getString("success"));
String status = jObject.getString("success").toString();
if(status.equals("1")) {
JSONArray array = jObject.getJSONArray("reports");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
Log.d("id", jo.getString("id"));
Log.d("image", jo.getString("image"));
images.add(jo.getString("image"));
//adapter().notifyDataSetChanged();
/*HashMap<String, String> department = new HashMap<>();
department.put("id", jo.getString("id"));
department.put("name", jo.getString("name"));
list.add(department);*/
}
adapter.updateList(images);
////intent.putExtra("result", result);
}
else
Toast.makeText(getApplicationContext(), "No doctor found", Toast.LENGTH_LONG).show();
}
//// else
//// intent.putExtra("result", "NOTHONG FIND");
} catch (JSONException e) {
e.printStackTrace();
}
//// startActivity(intent);
}
package android.hmkcode.com.apicall;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
/**
* Created by umar on 3/7/2016.
*/
public class ViewPagerAdapter extends PagerAdapter {
Context c;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
public ViewPagerAdapter(Context c, ArrayList<String> imagePaths) {
Log.d("CUEW OAFE ADAORER", "constructor");
this._imagePaths = imagePaths;
Log.d("size",this._imagePaths.size()+"");
this.c = c;
}
@Override
public int getCount() {
return this._imagePaths.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.pager_item, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.image);
Picasso.with(c).load(_imagePaths.get(position)).into(imgDisplay);
(container).addView(viewLayout);
return viewLayout;
}
@Override
public void notifyDataSetChanged() {
Log.d("NOTFIY STATE CHANGED","NOTIFY STATE CHANGE");
super.notifyDataSetChanged();
}
@Override
public Parcelable saveState() {
return null;
}
/*public int getItemPosition(Object object) {
return POSITION_NONE;
}*/
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
(container).removeView((RelativeLayout) object);
}
public void updateList(ArrayList<String> images){
Log.d("updated list", "list update");
this._imagePaths = images;
Log.d("size",this._imagePaths.size()+"");
this.notifyDataSetChanged();
}
}
package android.hmkcode.com.apicall;
导入android.content.Context;
导入android.os.Parcelable;
导入android.support.v4.view.PagerAdapter;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入com.squareup.picasso.picasso;
导入java.util.ArrayList;
导入java.util.List;
/**
*由umar于2016年3月7日创建。
*/
公共类ViewPagerAdapter扩展了PagerAdapter{
上下文c;
私有ArrayList_ImagePath;
私人充气机;
public ViewPagerAdapter(上下文c、ArrayList ImagePath){
Log.d(“CUEW-OAFE-ADAORER”,“构造函数”);
这是。_imagepath=imagepath;
Log.d(“size”,this.\u imagepath.size()+”);
这个.c=c;
}
@凌驾
public int getCount(){
返回此项。_imagepath.size();
}
@凌驾
公共布尔值isViewFromObject(视图,对象){
返回视图==(对象);
}
@凌驾
公共对象实例化项(视图组容器,int位置){
图像视图imgDisplay;
充气机=(充气机)c
.getSystemService(上下文布局\充气机\服务);
视图布局=充气机。充气(R.layout.pager_项目,容器,
假);
imgDisplay=(ImageView)viewLayout.findViewById(R.id.image);
毕加索.with(c).load(_imagepath.get(position)).into(imgDisplay);
(容器).添加视图(视图布局);
返回视图布局;
}
@凌驾
public void notifyDataSetChanged(){
Log.d(“通知状态更改”、“通知状态更改”);
super.notifyDataSetChanged();
}
@凌驾
公共包裹存储状态(){
返回null;
}
/*public int getItemPosition(对象){
返回位置\无;
}*/
@凌驾
公共无效恢复状态(可包裹状态,类装入器){
}
@凌驾
公共项(视图组容器、int位置、对象){
(容器).removeView((RelativeLayout)对象);
}
公共无效更新列表(ArrayList图像){
Log.d(“更新列表”、“列表更新”);
这。_imagepath=图像;
Log.d(“size”,this.\u imagepath.size()+”);
this.notifyDataSetChanged();
}
}
数据已更新,但UI仍然为空
每一个建议都被采纳了
谢谢大家
为什么要设置一个空列表?只需显示一些文本或进度加载器,直到json到达,然后设置适配器一次。但它不允许我这样做。因为onCreate活动首先使用空适配器分配其。RESTAPI响应在这之后。就是这样!从onCreate中删除适配器代码,并在响应到达时执行。我这样做了,但没有更新。为什么要设置空列表?只需显示一些文本或进度加载器,直到json到达,然后设置适配器一次。但它不允许我这样做。因为onCreate活动首先将其分配给e