Amit Nair Amit Nair - 4 months ago 250
JSON Question

Image Slider image not loading (ViewPager + Picasso)

I'm creating a Image Slider using Picasso + Viewpager.
I am able to swipe the slider base on the image size(counts).
But no images from array has been loaded even though i can swipe the screen with default images.
Please correct my mistakes if any founded. I am new to android.

Thanks in advance

My json array is like this

[
{
"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"
}
]
}
]


In the postexecute of my activity i am setting the adapter as follow

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());
}
}
}


My ViewpagerAdapter is

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;
}
}


My secondcar_detail.xml is

<?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>

Answer

Replace get method call

vehicleSliderIamges.get(i).toString()

to getJSONObject method call:

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

get method call returns below strings:

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