Search code examples
androidjsonviewflipper

How to make slideshow for images and text using viewflipper from json


//mainactivity.xml

<ViewFlipper 
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="279dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/background_light"
    android:gravity="center" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView7"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="14dp"
        android:text="Event Name" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="Venue, Date"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="Description"
        android:textSize="12sp" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="14dp"
        android:src="@drawable/demo" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="Event of the Week" />

</RelativeLayout>
</ViewFlipper>

I want to implement slideshow for both images and text for the above layout. And the values are from my server using json. How can i implement this. I am stuck here. does any body knows the answer.

Any body have reference link..


Solution

  • Here is the basic process you'll want to follow.

    1. Download JSON data: You'll need to use a background thread and an HTTP client to do this. Perhaps AsyncTaskand DefaultHttpClient

    2. Parse JSON data: Once you have download the JSON you'll need to create JSONObjects and parse them into data model classes to store the data. Your model class might look something like this:

    public class SlideshowImage {
        public String text;
        public String imageUrl;
    } 
    

    Ideally you will end up with a List of SlideshowImages from the JSON.

    3. Store the Data if needed (optional): If you want to store these model classes in a datbase that can also be done. It may not be needed however if you intend to load this information live every time.

    4 Use the list of 'SlideshowImages' to populate your view: Finally you can access the SlideshowImage classes to load the data into each of your TextView and ImageViews.

    SlideshowImage model = slideshowImageList.get(0);
    textView1.setText(model.text);
    

    You'll also need to download the imageUrls to get Bitmaps so you can setup your ImageViews. You should refer to this question for help with that.