Reputation: 4064
I am using android ViewPager.
I am setting viewpager with empty list in the beginning because I only want to show the dynamic data which I will fetch from REST API response.
So while giving an empty list of adapter. there will be 0 page in the beginning. So beside that I am running AsynTask which will fetch the data in JSON format from server.
I want to set the empty View Pager count with the array size of json response. So total number of pages = size of json array
But When I try to update the adapter. Its does nothing . Stay showing only empty screen.
I am having trouble in updating the view pager.
onPostExecute Method of AsynTask.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);
}
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();
}
}
Data got updated but UI is still empty.
Every one suggestion is apprreciated.
Thank you all.
Upvotes: 0
Views: 1179
Reputation: 257
Upvotes: 2