在View Pager中传递Json图像

时间:2015-11-15 05:19:57

标签: java android json

我正在尝试在视图寻呼机中传递Json数据,它没有显示任何错误。但它也没有在视图寻呼机中显示图像,我无法理解我的错误

JSON:http://www.souqalkhaleejia.com/webapis/banners.php

Banner.java

h1 {
  color: red;
}

Adapter.java

public class Banner extends Fragment {
    ViewPager bannerpager;
    ArrayList<Data> bannerdta = new ArrayList<Data>();
    BannerAdapter bannerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View bannerp = inflater.inflate(R.layout.banner, container, false);
        bannerpager = (ViewPager) bannerp.findViewById(R.id.bannerpager);
        bannerpager.setAdapter(bannerAdapter);
        bannerAdapter = new BannerAdapter(bannerdta, getActivity());
        loadbanner();
        return bannerp;
    }

    private void loadbanner() {
        String bannerurl = "http://www.souqalkhaleejia.com/webapis/banners.php";
        JsonObjectRequest bannerreq = new JsonObjectRequest(Request.Method.GET, bannerurl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray banners = response.getJSONArray("banners");
                    for (int i = 0; i < banners.length(); i++) {
                        JSONObject banner1 = banners.getJSONObject(i);
                        Data banndata = new Data();
                        banndata.setBannerimages(banner1.getString("image"));
                        bannerdta.add(banndata);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                bannerAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "" + error, Toast.LENGTH_SHORT).show();
            }
        });
        AppController.getInstance().addToRequestQueue(bannerreq);
    }
}

我将viewpager从另一个类包含到主页 Home.xml

public class BannerAdapter extends PagerAdapter {
    Context cntx;
    private ArrayList<Data> blist;
    private LayoutInflater binflater;

    public BannerAdapter(ArrayList<Data> blist, Context cntx) {
        this.blist = blist;
        this.cntx = cntx;
        binflater= (LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override

    public int getCount() {
        return blist.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view=binflater.inflate(R.layout.banner_layout,container,false);
        NetworkImageView bannerimage= (NetworkImageView) view.findViewById(R.id.bannerimage);
        Data bannerdata=blist.get(position);
        ImageLoader imageLoader=AppController.getInstance().getImageLoader();
        bannerimage.setImageUrl(bannerdata.getBannerimages(),imageLoader);
        view.setTag(bannerdata);
        container.addView(view);
        Log.i("Banner", "instantiateItem() [position: " + position + "]" + " childCount:" + container.getChildCount());
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        Log.i("Banner", "destroyItem() [position: " + position + "]" + " childCount:" + container.getChildCount());
    }

    @Override
    public int getItemPosition(Object object) {
        Data data= (Data) ((View) object).getTag();
        int position=blist.indexOf(data);
        if(position>=0){
            return position;
        }else {
            return POSITION_NONE;
        }
    }
}

banner.xml(我在这里写过viewpager xml并包含在Home布局中)

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include
            android:id="@+id/hmebar"
            layout="@layout/toolbar" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            >
            <include layout="@layout/banner"/>
        </FrameLayout>

    </LinearLayout>

    <ListView
        android:id="@+id/nav_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/hover"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

banner_layout.xml(单个viewpager是图像)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:id="@+id/bannerpager"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您未将bannerdta更新为bannerAdapter,因此bannerAdapter.notifyDataSetChanged()将无效。
您可以在updateData(Data banndata)中添加方法BannerAdapter.class pubilc void updateData(Data banndata) { blist.add(banndata); notifyDataSetChanged(); }
并在呼叫网络请求成功后,使用bannerAdapter.updateData(banndata)