jsonslideshowandroid-pageradapter

Why my pager adapter cant show the data that I load from openweather(as json format) cant show properly


Why my pager adapter cant show the data that I load from openweather(as json format) cant show properly. I got five page in the slide view and the data in the first page will not show unless I load to the third page and the data in second page will show only when I load to the fourth page.

This is the coding of my slider java class

public class SliderAdapter extends PagerAdapter {

Context context;
LayoutInflater layoutInflater;
private static final String TAG = "SliderAdapter";

public SliderAdapter(Context context)
{
    this.context=context;
}

public double[] slide_headings= new double[50];
public String[] slide_desc=new String[50];
public String[] slide_images= new String[50];
public static final String URL_DATA = "http://api.openweathermap.org/data/2.5/forecast?id=1732698&appid=4bdfb7127d4a85742cfbb201078ba566";


@Override
public int getCount() {
    return 5;
}

@Override
public boolean isViewFromObject(View view,Object o) {
    return view== o;
}

@Override
public Object instantiateItem(ViewGroup container,int position)
{
    getData();
    layoutInflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.inflate(R.layout.slide_layout,container,false);
    ImageView slideImageView=view.findViewById(R.id.imageView);

    TextView slideHeading=view.findViewById(R.id.textView);
    TextView slideDescription=view.findViewById(R.id.textView2);
    Glide.with(context).load(slide_images[position]).into(slideImageView);

    slideHeading.setText(Double.toString(slide_headings[position]));
    slideDescription.setText(slide_desc[position]);
    container.addView(view);
    return view;
}

private void getData() {
    final ProgressDialog progressDialog = new ProgressDialog(context);
    progressDialog.setMessage("Loading data.....");
    progressDialog.show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            URL_DATA,

            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        for (int i = 0; i < jsonObject.length(); i++) {
                            JSONArray JA = (JSONArray) jsonObject.get("list");
                            for (int j = 0; j < JA.length(); j++) {
                                JSONObject JO = JA.getJSONObject(j);
                                JSONObject jo = (JSONObject) JO.get("main");
                                slide_headings[j] = jo.getDouble("temp");
                                JSONArray ja = JO.getJSONArray("weather");
                                for (int k = 0; k < ja.length(); k++) {
                                    JSONObject o = ja.getJSONObject(k);
                                    slide_desc[j] = o.getString("main");
                                    slide_images[j] = "http://openweathermap.org/img/w/" + o.getString("icon") + ".png";
                                }
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            //this method will run when there is error sending the request
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    progressDialog.dismiss();
                    Toast.makeText(context, volleyError.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);
}


@Override
public void destroyItem(ViewGroup container, int position,Object object) {
    container.removeView((RelativeLayout)object);
}

}

This is my example of my main java class

public class MainActivity extends AppCompatActivity {

private ViewPager mSLideViewPager;
private LinearLayout mDotLayout;
private SliderAdapter sliderAdapter;
private TextView[] mDots;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSLideViewPager=findViewById(R.id.slideViewPager);
    mDotLayout=findViewById(R.id.dotsLayout);

    sliderAdapter=new SliderAdapter(this);
    mSLideViewPager.setAdapter(sliderAdapter);

    addDotsIndicator(0);

    mSLideViewPager.addOnPageChangeListener(viewListener);

}

public void addDotsIndicator(int position){
    mDotLayout.removeAllViews();
    mDots=new TextView[5];

    for(int i=0;i<mDots.length ;i++)
    {
        mDots[i] = new TextView(this);
        mDots[i].setText(Html.fromHtml("&#8226"));
        mDots[i].setTextSize(35);
        mDots[i].setTextColor(getResources().getColor(R.color.colorTransparentWhite));

        mDotLayout.addView(mDots[i]);
    }
    if(mDots.length >0){
        mDots[position].setTextColor(getResources().getColor(R.color.colorWhite));
    }

}

ViewPager.OnPageChangeListener viewListener= new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        addDotsIndicator(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
};

}


Solution

  • I have used array list instead of array and this solve my problems