图像滑块图像未加载(ViewPager + Picasso)

时间:2016-07-18 13:51:03

标签: android json

我正在使用Picasso + Viewpager创建一个Image Slider。 我可以根据图像大小(计数)滑动滑块。 但即使我可以使用默认图像滑动屏幕,也没有加载阵列中的图像。 如果有任何建议,请纠正我的错误。我是android的新手。

提前致谢

我的json数组就像这样

 [
 {
"guid": "C410B2AA-EC03-29E4-F5C0-CB48510ED9E5",
"id": "2",
"created_on": "2016-05-17 13:13:22",
"car_name": "Hundai",
"model": "C001",
"version": "i20",
"make_year": "2012",
"kms_driven": "2000",
"city": "Bangalore",
"pincode": "560072",
"expected_price": "200000",
"name": "Vishnu",
"email": "dreamvishnu@gmail.com",
"mobile": "9863265358",
"image": [
  {
    "newimage": "https://images.cardekho.com/car-images/carexteriorimages/large/Ford/Ford-Mustang/ford-mustang-exterior-047.jpg"
  },
  {
    "newimage": "https://www.enterprise.com/content/dam/global-vehicle-images/cars/FORD_FOCU_2012-1.png"
  },
  {
    "newimage": "https://imagecdn8.cartrade.com/img/800x600/car-data/big/bmw-i8-default-image.png"
  }
]
 }
 ]

在我的活动发布后,我将适配器设置如下

 protected void onPostExecute(Void v) {

        try {
            JSONArray object = new JSONArray(result);
            ArrayList<String> items = new ArrayList<String>();

            try {
                for (int i = 0; i < object.length(); i++) {
                    JSONObject obj = object.getJSONObject(i);
                    vehicleSliderIamges = obj.getJSONArray("image");
                    guid = obj.getString("guid");
                    name = obj.getString("car_name");
                    model = obj.getString("version");
                    price = obj.getString("expected_price");
                    km = obj.getString("kms_driven");
                    fuel = obj.getString("model");
                    year = obj.getString("make_year");
                    location = obj.getString("city");
                    gear = obj.getString("name");



                }
                ArrayList<String> list = new ArrayList<String>();
                if (vehicleSliderIamges != null) {
                    int len = vehicleSliderIamges.length();
                    for (int i=0;i<len;i++){
                        list.add(vehicleSliderIamges.get(i).toString());
                    }
                }
                viewPagerAdapter = new ViewPagerAdapter(SecondHandCarDetailsActivity.this,list);
                viewPager.setAdapter(viewPagerAdapter);
                vehicleName.setText(name);
                vehicleModel.setText(model);
                vehiclePrice.setText(price);
                vehicleYear.setText(year);
                vehicleFuel.setText(fuel);
                vehicleKm.setText(km);
                vehicleGear.setText(gear);
                vehicleLocation.setText(location);



                this.progressDialog.dismiss();





            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }
}

我的ViewpagerAdapter

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;


public class ViewPagerAdapter extends PagerAdapter {
private  Context context;
private ArrayList<String> IMAGES = new ArrayList<>();


public ViewPagerAdapter(Context context, ArrayList<String> IMAGES) {
    this.IMAGES = IMAGES;
    this.context = context;
}

@Override
public int getCount() {
    return IMAGES.size();
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

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


@Override
public Parcelable saveState() {
    return null;
}

@Override
public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.imagepager_layout,null);
    ((ViewPager) collection).addView(view);
    final ImageView img = (ImageView) view.findViewById(R.id.img);
    Picasso.with(context)
            .load(IMAGES.get(position))
            .placeholder(R.drawable.loading)
            .into(img);
    return view;
}
}

我的secondcar_detail.xml是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/white"

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

3 个答案:

答案 0 :(得分:2)

替换get方法调用

vehicleSliderIamges.get(i).toString()

getJSONObject方法调用:

vehicleSliderIamges.getJSONObject(i).get("newimage").toString()

get方法调用返回字符串:

{"newimage":"https:\/\/images.cardekho.com\/car-images\/carexteriorimages\/large\/Ford\/Ford-Mustang\/ford-mustang-exterior-047.jpg"}

答案 1 :(得分:0)

您忘记使用notifyDataSetChanged()通知适配器中的更改:

viewPagerAdapter = new ViewPagerAdapter(SecondHandCarDetailsActivity.this,list);
viewPager.setAdapter(viewPagerAdapter);
viewPagerAdapter.notifyDataSetChanged();

答案 2 :(得分:0)

显然毕加索不支持https开箱即用。请参阅问题here

1)您可以获得图片的http网址或

2)您使用类似OkHttpClient

的HTTP客户端
OkHttpClient client = new OkHttpClient();
client.setProtocols(Arrays.asList(Protocol.HTTP_11));
Picasso picasso = new Picasso.Builder(context)
    .downloader(new OkDownloader(client))
    .build();

使用以下代码:

 ArrayList<String> list = new ArrayList<String>();
                if (vehicleSliderIamges != null) {
                      for (JSONObject o: vehicleSliderIamges){
                        list.add(o.getString("newimage");
                    }
                }