为什么我的寻呼机适配器无法显示我从openweather加载的数据(以json格式)无法正确显示

时间:2019-03-29 08:49:01

标签: json slideshow android-pageradapter

为什么我的寻呼机适配器无法显示我从openweather加载的数据(以json格式)无法正确显示。我在幻灯片视图中有五页,除非加载到第三页,否则第一页中的数据将不会显示,只有加载到第四页时,第二页中的数据才会显示。

这是我的滑块Java类的编码

公共类SliderAdapter扩展了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);
}

}

这是我的主要Java类的示例

公共类MainActivity扩展了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) {

    }
};

}

1 个答案:

答案 0 :(得分:0)

我用数组列表代替了数组,这解决了我的问题