Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的寻呼机适配器不能显示我从openweather加载的数据(json格式)不能正确显示_Json_Slideshow_Android Pageradapter - Fatal编程技术网

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

为什么我的寻呼机适配器不能显示我从openweather加载的数据(json格式)不能正确显示,json,slideshow,android-pageradapter,Json,Slideshow,Android Pageradapter,为什么我的寻呼机适配器不能显示我从openweather加载的数据(json格式)不能正确显示。幻灯片视图中有五页,第一页中的数据只有加载到第三页时才会显示,第二页中的数据只有加载到第四页时才会显示 这是我的slider java类的编码 公共类SliderAdapter扩展了PagerAdapter{ Context context; LayoutInflater layoutInflater; private static final String TAG = "SliderAdapter"

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

这是我的slider 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);
}
语境;
LayoutInflater LayoutInflater;
私有静态最终字符串标记=“SliderAdapter”;
公共滑块适配器(上下文)
{
this.context=context;
}
公共双[]幻灯片标题=新双[50];
公共字符串[]幻灯片描述=新字符串[50];
公共字符串[]幻灯片图片=新字符串[50];
公共静态最终字符串URL_DATA=”http://api.openweathermap.org/data/2.5/forecast?id=1732698&appid=4bdfb7127d4a85742cfbb201078ba566";
@凌驾
public int getCount(){
返回5;
}
@凌驾
公共布尔值isViewFromObject(视图,对象o){
返回视图==o;
}
@凌驾
公共对象实例化项(视图组容器,int位置)
{
getData();
layoutInflater=(layoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=布局更平。充气(右布局。幻灯片布局,容器,假);
ImageView slideImageView=view.findViewById(R.id.ImageView);
TextView slideHeading=view.findViewById(R.id.TextView);
TextView slideDescription=view.findViewById(R.id.textView2);
使用(上下文)滑动。加载(幻灯片图像[位置])到(幻灯片图像视图);
slideHeading.setText(Double.toString(幻灯片标题[位置]);
slideDescription.setText(幻灯片描述[位置]);
container.addView(视图);
返回视图;
}
私有void getData(){
final ProgressDialog ProgressDialog=新建ProgressDialog(上下文);
progressDialog.setMessage(“加载数据…”);
progressDialog.show();
StringRequest StringRequest=新StringRequest(Request.Method.POST,
URL\u数据,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串s){
progressDialog.disclose();
试一试{
JSONObject JSONObject=新的JSONObject;
for(int i=0;i
}

这是我的主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) {

    }
};
private ViewPager mSLideViewPager;
私人线路布局;
专用滑块适配器滑块适配器;
私有文本视图[]mdot;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSLideViewPager=findviewbyd(R.id.slideviewpage);
mDotLayout=findviewbyd(R.id.dotsLayout);
sliderAdapter=新的sliderAdapter(此);
mSLideViewPager.setAdapter(滑动适配器);
addDotsIndicator(0);
mSLideViewPager.addOnPageChangeListener(viewListener);
}
公共无效添加点指示器(整数位置){
mDotLayout.removeallview();
mDots=新文本视图[5];
对于(int i=0;i0){
mDots[position].setTextColor(getResources().getColor(R.color.colorWhite));
}
}
ViewPager.OnPageChangeListener viewListener=新建ViewPager.OnPageChangeListener(){
@凌驾
页面上的公共无效已滚动(整数i、浮点v、整数i1){
}
@凌驾
已选择页面上的公共无效(int i){
addDotsIndicator(i);
}
@凌驾
页面上的公共无效状态已更改(int i){
}
};

}

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

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