我正在使用android ViewPager。Android Empty ViewPager更新,其餘API響應
我在開始時將viewpager設置爲空列表,因爲我只想顯示將從REST API響應中獲取的動態數據。
所以,同時給出一個空的適配器列表。一開始會有0頁。所以除此之外,我正在運行AsynTask,它將從服務器獲取JSON格式的數據。
我想設置空的視圖尋呼機計數與數組大小的json響應。 因此總頁數= json數組的大小
但是當我嘗試更新適配器時。它什麼都不做。保持只顯示空屏幕。
我在更新視圖尋呼機時遇到問題。
AsynTask.java的onPostExecute方法
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);
}
ViewPagerAdapter.java
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();
}
}
數據得到了更新,但用戶界面仍然是空的。
每一個建議都被認可。
謝謝大家。
你爲什麼設置一個空列表?只需顯示一些文本或進度加載器,直到json到達,然後設置適配器一次。 –
但它不讓我這樣做。因爲onCreate活動首先將其分配給空適配器。並在此之後休息api響應。 –
就是這樣!從onCreate中刪除適配器代碼,並在響應到達時執行它。 –