Android Retrofit ListView Tutorial and Example


Android Retrofit : MySQL - MasterDetail ListView with Images and Text.

We want to see how to connect android app to mysql database using Retrofit then download images and text saved in that database and render in our custom listview with cardviews. Then when the user clicks a single cardview, we will open a detail activity showing more details of the item.

However first we need to introduce some APIs, classes and concepts.

What is Retrofit.

Retrofit is a type safe HTTP Client for android and java. It allows us make HTTP requests from within our android application. Those HTTP requests then empower our android application to talk to other computers and exchange data over the HTTP protocol.

Don't underestimate this ability as it makes apps very powerful and with almost limitless capabilities. Imagine for example android apps without this capability. The apps would be complete offline and disconnected. That means you would be able to do the following tasks:

  1. Update or even Install apps via playstore.
  2. Browse internet even via browsers.
  3. Chat via Whatsapp, facebook, google plus etc.
  4. Watch YouTube and other online videos.
  5. Perform online financial transactions via companies like Stripe and PayPal.
  6. View and Download your bank's financial statements.
  7. Purchase products from ecommerce giants like AliBaba, Jumia etc.

All the above and thousands and thousands more capabilities would not be happening had we not have the ability to communicate via HTTP from our android apps. However that communication is two-way. You need a client and server. The client performs a request and the server responds appropriately.

Most server code is written in PHP code and the popular database used is MySQL. However android side we use java and a wealth of HTTP Clients. Retrofit is probably in the top three of HTTP clients currently.

Read more about Retrofit here.

What is MySQL

MySQL is a fast and open source RDBMS database system that uses SQL language. It's probably the most popular database server. We use it to store data in the server.

Mostly mysql is manipulated via SQL statements. However to use it in a real application we also need a general language. These languages include PHP, Python, Java, C#, Ruby, Kotlin etc.

In this tutorial we will use PHP.

What is PHP?

PHP is a server side programming language that is known to be fast yet beginner friendly. It is the most popular server side programming language.

This is no mean feat as remember it's beaten powerful general purpose languages like Java, C# and Python for that role. The main reason for that is the fact that PHP is one of the easiest languages to get started with. Moreover it can seamlessly be embedded in webpages in a way that is even more straight forward than Javascript.

However PHP is not a toy language. It is a powerful language that has advanced features like Object oriented capabilities. It's being used to power the most demanding of web applications like Facebook and Wikipedia.

We use PHP to write our server code. And yes we write clean object oriented code you will love.

What is ListView?

ListView is our adapterview. In other words it is our list component. We use it to display our lists of data. That list can then be easily scrolled. ListViews allow us display large data sets. An alternative to ListView is recyclerView.

Most people love using listviews to display images and text. Those involve placing an imageview and textview in a cardview. Then using the cardview as the item view for the listview.

It's what we do here.

You can read more about ListViews here.

1. Android Retrofit - MySQL MasterDetail - ListView Images and Text

Android Retrofit : MySQL - MasterDetail ListView with Images and Text.

We want to see how to connect android app to mysql database using Retrofit then download images and text saved in that database and render in our custom listview with cardviews. Then when the user clicks a single cardview, we will open a detail activity showing more details of the item.

In actuality, normally we store the images in the file system and the url as well as the text in the database. This is the more efficient approach. You don't want to be bloating your database with large blobs of data. Morever it's easier and efficient to update and even delete binary data from the file system than blobs in database.

Here's the demo.

Android Retroft ListView mySQL Images Text

Here's the landscape mode:

Android Retroft ListView mySQL Images Text

General App Structure
No. Name Role
1. MainActivity.java Our main activity. It is also our master view ad will contain our listview
2. DetailsActivity.java Our details activity. It is our detail view and will contain several widgets displaying the details.
3. activity_main.xml Our main activity's layout.
4. activity_detail.xml Our detail activity's layout.
5. build.gradle We will add dependencies here.
6. AndroidManifest.xml We will add intenet connectivity permission here.

1. Setup

(a). PHP Code

First we need to prepare a database. I used XAMPP with PHPMyAdmin to create my database.

Here's my table structure: Android PHP MySQL Retrofit ListView

Then in XAMPP or WAMP or whatever server you are using go to the root directory of your project and add php code and an image folders in a given directory. For example see mine here:

Android PHP MySQL Retrofit ListView

Then add images to the images folder:

Android PHP MySQL Retrofit ListView

In the index.php file we will write Object Oriented PHP code to talk to our database and return us data. We start by creating a class called Constants that will contain our database server name, database name, database user name and password.

root and ` are the default username and password for XAMPP and WAMP. Use them only in learning. In production create a new user and give him/her a strong password. Then specify them in theConstants` class.

How to Create a MySQL Database Table via SQL

Here's the SQL code we used to create our database table:

CREATE TABLE `spacecraftsdb`.`spacecraftstb` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(150) NOT NULL , `propellant` VARCHAR(150) NOT NULL , `destination` VARCHAR(150) NOT NULL , `image_url` VARCHAR(150) NOT NULL , `technology_exists` TINYINT NOT NULL , PRIMARY KEY (`id`) ) ENGINE = InnoDB;

How to Insert Into MySQL Table

You can then insert using a statement like this:

INSERT INTO `spacecraftsdb`.`spacecraftstb` (`id`, `name`, `propellant`, `destination`, `image_url`, `technology_exists`) VALUES (NULL, 'Voyager A', 'Warp Drive', 'Alpha Centauri A', 'voyager-a.jpg', '0'), (NULL, 'Voyager B', 'Plasma Ions', 'Proxima Centauri', 'voyager-b.jpg', '1')

This PHP Code will fetch us all our data in mysql database and print it out as json.

<?php

class Constants
{
    //DATABASE DETAILS
    static $DB_SERVER="localhost";
    static $DB_NAME="spacecraftsDB";
    static $USERNAME="root";
    static $PASSWORD="";

    //STATEMENTS
    static $SQL_SELECT_ALL="SELECT * FROM spacecraftsTB";

}

class Spacecrafts
{
    /*******************************************************************************************************************************************/
    /*
       1.CONNECT TO DATABASE.
       2. RETURN CONNECTION OBJECT
    */
    public function connect()
    {
        $con=new mysqli(Constants::$DB_SERVER,Constants::$USERNAME,Constants::$PASSWORD,Constants::$DB_NAME);
        if($con->connect_error)
        {
            // echo "Unable To Connect"; - For debug
            return null;
        }else
        {
            //echo "Connected"; - For debug
            return $con;
        }
    }
    /*******************************************************************************************************************************************/
    /*
       1.SELECT FROM DATABASE.
    */
    public function select()
    {
        $con=$this->connect();
        if($con != null)
        {
            $result=$con->query(Constants::$SQL_SELECT_ALL);
            if($result->num_rows>0)
            {
                $spacecrafts=array();
                while($row=$result->fetch_array())
                {
                    array_push($spacecrafts, array("id"=>$row['id'],"name"=>$row['name'],
                    "propellant"=>$row['propellant'],"destination"=>$row['destination'],
                    "image_url"=>$row['image_url'],"technology_exists"=>$row['technology_exists']));
                }
                print(json_encode(array_reverse($spacecrafts)));
            }else
            {
                print(json_encode(array("PHP EXCEPTION : CAN'T RETRIEVE FROM MYSQL. ")));
            }
            $con->close();

        }else{
            print(json_encode(array("PHP EXCEPTION : CAN'T CONNECT TO MYSQL. NULL CONNECTION.")));
        }
    }
}
$spacecrafts=new Spacecrafts();
$spacecrafts->select();

//end
(b). Build.gradle

Normally we have two build.gradle scripts. We are interested in the one located in the app folder rather than the project folder of your application.

Here we can add dependencies we will be using. Android Studio employs Gradle as the build system. One of it's roles then is to manage dependencies.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation "com.android.support:cardview-v7:28.0.0"
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
}

We have added cardview as our listview will comprise cardviews. Then those cardviews will contain other widgets like imageviews and textviews.

retrofit on the other hand is our HTTP Client so we add it. Remember Retrofit even though it's very popular, it is a third party library. Hence it and alongside the other following dependencies will need internet connectivity.

converter-gson is our converter for retrofit. It will map our JSON Objects into type safe Java POJO classes. This allows us to avoid having to manually parse our JSON data. Instead they get mapped to model classes via the converter-gson.

picasso on the other hand is our image loader library. Loading images from internet, it turns out, can be a little bit involved. Hence it makes sense to avoid having to re-invent the wheel and use a proven battle tested library. Well Picasso is a top two image loader library and we use it here.

(b). AndroidManifest.xml

You need to add permission to your androd manifest file.

<uses-permission android:name="android.permission.INTERNET"/>

In this case we've added the internet connectivity permission as we are connecting to a network. Failure to do this will result in your application not being allowed to access network.

Also don't forget to register the DetailsActivity in your android manifest:

<application>
        <activity android:name=".MainActivity">
        </activity>
        <activity android:name=".DetailsActivity" android:parentActivityName=".MainActivity"/>
    </application>

If you don't register activity's then your app will crash if you attempt to open that activity.

2. Layouts

We have three layoust:

  1. activity_main
  2. activity_detail
  3. model
(a). activity_main.xml

This is the main activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/headerTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Retrofit ListView MySQL"
        android:padding="5dp"
        android:textAlignment="center"
        android:textStyle="bold"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/colorAccent" />

    <ProgressBar
        android:id="@+id/myProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:indeterminateBehavior="cycle"
        android:visibility="gone" />

    <ListView
        android:id="@+id/mListView"
        android:layout_weight="0.5"
        android:numColumns="auto_fit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
(b). activity_detail.xml

This is the detail activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#009688"
    tools:context=".DetailsActivity"
    tools:showIn="@layout/activity_detail">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        card_view:cardCornerRadius="3dp"
        card_view:cardElevation="5dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/teacherDetailImageView"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:layout_alignParentTop="true"
                    android:padding="5dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/placeholder" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/nameLabel"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="NAME : "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/nameDetailTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="Voyager"
                            android:textAppearance="?android:attr/textAppearanceLarge"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/dateLabel"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="DATE : "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/dateDetailTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="Today"
                            android:textAppearance="?android:attr/textAppearanceLarge"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/destinationLabel"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="DESTINATION : "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/destinationDetailTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="KY Cygni"
                            android:textAppearance="?android:attr/textAppearanceLarge"/>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/propellantLabel"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="PROPELLANT : "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/propellantDetailTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="Chemical"
                            android:textAppearance="?android:attr/textAppearanceLarge"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/techExistsLabel"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:padding="5dp"
                            android:text="TECH EXISTS? : "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />

                        <CheckBox
                            android:id="@+id/techExistsDetailCheckBox"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:padding="5dp"/>
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </android.support.v7.widget.CardView>
</RelativeLayout>
(c). model.xml

This layout will get inflated into the item view for our listview.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="5dp"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/spacecraftImageView"
            android:padding="5dp"
            android:scaleType="fitXY"
            android:src="@drawable/placeholder" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Spacecraft Name"
            android:id="@+id/nameTextView"
            android:padding="5dp"
            android:textColor="@color/colorAccent"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/spacecraftImageView" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Propellant"
            android:textStyle="italic"
            android:id="@+id/propellantTextView"
            android:padding="5dp"
            android:layout_alignBottom="@+id/spacecraftImageView"
            android:layout_toRightOf="@+id/spacecraftImageView" />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/myCheckBox"
            android:text="Tech Exists?"
            android:layout_alignParentRight="true" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

3. Java Code

We have three layoust:

  1. MainActivity
  2. DetailsActivity
(a). MainActivity.java

This is the main activity.

package info.camposha.retrofitmysqllistview;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.annotations.SerializedName;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class MainActivity extends AppCompatActivity {
    //private static final String BASE_URL = "http://10.0.2.2";
    private static final String BASE_URL = "http://192.168.12.2";
    private static final String FULL_URL = BASE_URL+"/PHP/spacecrafts/";
    class Spacecraft {
        @SerializedName("id")
        private int id;
        @SerializedName("name")
        private String name;
        @SerializedName("propellant")
        private String propellant;
        @SerializedName("image_url")
        private String imageURL;
        @SerializedName("technology_exists")
        private int technologyExists;

        public Spacecraft(int id, String name, String propellant, String imageURL, int technologyExists) {
            this.id = id;
            this.name = name;
            this.propellant = propellant;
            this.imageURL = imageURL;
            this.technologyExists = technologyExists;
        }

        /*
         *GETTERS AND SETTERS
         */
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPropellant() {
            return propellant;
        }

        public String getImageURL() {
            return imageURL;
        }
        public int getTechnologyExists() {
            return technologyExists;
        }
        @Override
        public String toString() {
            return name;
        }
    }

    interface MyAPIService {

        @GET("/PHP/spacecrafts")
        Call<List<Spacecraft>> getSpacecrafts();
    }

    static class RetrofitClientInstance {

        private static Retrofit retrofit;

        public static Retrofit getRetrofitInstance() {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }
    }

    class ListViewAdapter extends BaseAdapter{

        private List<Spacecraft> spacecrafts;
        private Context context;

        public ListViewAdapter(Context context,List<Spacecraft> spacecrafts){
            this.context = context;
            this.spacecrafts = spacecrafts;
        }

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

        @Override
        public Object getItem(int pos) {
            return spacecrafts.get(pos);
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            if(view==null)
            {
                view=LayoutInflater.from(context).inflate(R.layout.model,viewGroup,false);
            }

            TextView nameTxt = view.findViewById(R.id.nameTextView);
            TextView txtPropellant = view.findViewById(R.id.propellantTextView);
            CheckBox chkTechExists = view.findViewById(R.id.myCheckBox);
            ImageView spacecraftImageView = view.findViewById(R.id.spacecraftImageView);

            final Spacecraft thisSpacecraft= spacecrafts.get(position);

            nameTxt.setText(thisSpacecraft.getName());
            txtPropellant.setText(thisSpacecraft.getPropellant());
            chkTechExists.setChecked( thisSpacecraft.getTechnologyExists()== 1);
            chkTechExists.setEnabled(false);

            if(thisSpacecraft.getImageURL() != null && thisSpacecraft.getImageURL().length()>0)
            {
                Picasso.get().load(FULL_URL+"/images/"+thisSpacecraft.getImageURL()).placeholder(R.drawable.placeholder).into(spacecraftImageView);
            }else {
                Toast.makeText(context, "Empty Image URL", Toast.LENGTH_LONG).show();
                Picasso.get().load(R.drawable.placeholder).into(spacecraftImageView);
            }

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, thisSpacecraft.getName(), Toast.LENGTH_SHORT).show();
                    String techExists="";
                    if(thisSpacecraft.getTechnologyExists()==1){
                        techExists="YES";
                    }else{
                        techExists="NO";
                    }
                    String[] spacecrafts = {
                            thisSpacecraft.getName(),
                            thisSpacecraft.getPropellant(),
                            techExists,
                            FULL_URL+"/images/"+thisSpacecraft.getImageURL()
                    };
                    openDetailActivity(spacecrafts);
                }
            });

            return view;
        }
        private void openDetailActivity(String[] data) {
            Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
            intent.putExtra("NAME_KEY", data[0]);
            intent.putExtra("PROPELLANT_KEY", data[1]);
            intent.putExtra("TECHNOLOGY_EXISTS_KEY", data[2]);
            intent.putExtra("IMAGE_KEY", data[3]);
            startActivity(intent);
        }
    }

    private ListViewAdapter adapter;
    private ListView mListView;
    ProgressBar myProgressBar;

    private void populateListView(List<Spacecraft> spacecraftList) {
        mListView = findViewById(R.id.mListView);
        adapter = new ListViewAdapter(this,spacecraftList);
        mListView.setAdapter(adapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressBar myProgressBar= findViewById(R.id.myProgressBar);
        myProgressBar.setIndeterminate(true);
        myProgressBar.setVisibility(View.VISIBLE);

        /*Create handle for the RetrofitInstance interface*/
        MyAPIService myAPIService = RetrofitClientInstance.getRetrofitInstance().create(MyAPIService.class);

        Call<List<Spacecraft>> call = myAPIService.getSpacecrafts();
        call.enqueue(new Callback<List<Spacecraft>>() {

            @Override
            public void onResponse(Call<List<Spacecraft>> call, Response<List<Spacecraft>> response) {
                myProgressBar.setVisibility(View.GONE);
                populateListView(response.body());
            }
            @Override
            public void onFailure(Call<List<Spacecraft>> call, Throwable throwable) {
                myProgressBar.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, throwable.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}
(b). DetailsActivity.java

This is the details activity.

package info.camposha.retrofitmysqllistview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class DetailsActivity extends AppCompatActivity {

    TextView nameDetailTextView,propellantDetailTextView,dateDetailTextView,destinationDetailTextView;
    CheckBox techExistsDetailCheckBox;
    ImageView teacherDetailImageView;

    private void initializeWidgets(){
        nameDetailTextView= findViewById(R.id.nameDetailTextView);
        propellantDetailTextView= findViewById(R.id.propellantDetailTextView);
        dateDetailTextView= findViewById(R.id.dateDetailTextView);
        destinationDetailTextView=findViewById(R.id.destinationDetailTextView);
        techExistsDetailCheckBox= findViewById(R.id.techExistsDetailCheckBox);
        teacherDetailImageView=findViewById(R.id.teacherDetailImageView);
    }
    private String getDateToday(){
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd");
        Date date=new Date();
        String today= dateFormat.format(date);
        return today;
    }
    private String getRandomDestination(){
        String[] destinations={"VV Sephei","KY Cygni","Polaris","Betelgeuse","Aldebaran"};
        Random random=new Random();
        int index=random.nextInt(destinations.length-1);
        return destinations[index];
    }

    private void receiveAndShowData(){
        //RECEIVE DATA FROM ITEMS ACTIVITY VIA INTENT
        Intent i=this.getIntent();
        String name=i.getExtras().getString("NAME_KEY");
        String propellant=i.getExtras().getString("PROPELLANT_KEY");
        String technologyExists=i.getExtras().getString("TECHNOLOGY_EXISTS_KEY");
        String imageURL=i.getExtras().getString("IMAGE_KEY");

        //SET RECEIVED DATA TO TEXTVIEWS AND IMAGEVIEWS
        nameDetailTextView.setText(name);
        propellantDetailTextView.setText(propellant);
        dateDetailTextView.setText(getDateToday());
        destinationDetailTextView.setText(getRandomDestination());
        techExistsDetailCheckBox.setChecked(technologyExists.equalsIgnoreCase("YES"));
        techExistsDetailCheckBox.setEnabled(false);
        Picasso.get().load(imageURL).placeholder(R.drawable.placeholder).into(teacherDetailImageView);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        initializeWidgets();
        receiveAndShowData();
    }
}
Download

Download code below:

View Download
View Direct Download

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post