SlideShare a Scribd company logo
Android Apps Development – Training
Day 5
Previously
public void onClick(View arg0) {
String name = ETname.getText().toString();
String email = ETemail.getText().toString();
String phoneno = ETphoneno.getText().toString();
if(name.length()<1 || email.length()<1 || phoneno.length()<1){
//do something
}else{
dbhelper db= new dbhelper(getApplicationContext());
db.open();
Long response = db.insertdata(name, email, phoneno);
db.close();
}
}
AsyncTask
● Perform operations on a different thread
and also do this
Adding to that
public void onClick(View arg0) {
String name = ETname.getText().toString();
String email = ETemail.getText().toString();
String phoneno = ETphoneno.getText().toString();
if(name.length()<1 || email.length()<1 ||
phoneno.length()<1){
//do something
}else{
new DBAsync().execute(name, email, phoneno);
}
}
This is the AsyncTask
How do we do this ?
● We create a class inside our Activity that
extends the AsyncTask
● We use different methods it offers do perform
operations in the background:
– OnPreExecute()
● Generally used to load the progress bar
– doInBackground(Params... )
● All the logic is dumped here
– OnProgressUpdate()
● Called when publishProgress() is called in the doInBackground()
– onPostExecute(Result)
● Gives out the desired results
Class that extends the AsyncTask
public class DBAsync extends AsyncTask <String, String,
String>{
// code here
}
What are these?
● AsyncTask<Params, Progress, Result>
– Params: the input, what you pass into the
AsyncTask
– Progress: on updates, passed to onProgressUpdate()
– Result: the output from doInBackground() returned
to the onPostExecute()
onPreExecute
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//Declare progress as a global variable
progress = new ProgressDialog(Registration.this);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER
);
progress.setMessage("Filling up Database ...");
progress.show();
}
doInBackground
@Override
protected String doInBackground(String... string) {
// TODO Auto-generated method stub
dbhelper db = new
dbhelper(getApplicationContext());
db.open();
Long insert = db.insertdata(string[0], string[1],
string[2]);
db.close();
return null;
}
onPostExecute
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progress.dismiss();
Intent i = new Intent(Registration.this,
afterlogin.class);
i.putExtra("register", true);
startActivity(i);
}
Deeper with AsyncTask
● The first think you should understand:
– They rely on Java concepts of GENERICS and
VARARGS
GENERICS:
● Can declare of any type
VARARGS:
● Arrays… you can pass in number of values
Accessing HTTP
● There are two ways of accessing HTTP in
android:
– Using the HttpURLConnection
● Using the HttpURLConnection object and its method
– Using the HttpClient
● Use DefaultHttpClient, Http GET & POST,
HttpResponse, HttpEntity
● Both of these follow similar steps:
– Process the Http input
– Use InputStream and BufferedReader to iterate
through the http output.
DistanceMatrix API
● URL –
http://maps.googleapis.com/maps/api/distancematrix/json?
params
● Parameters:
– origins
● origins=Jamal,Kathmandu
– destinations
● destinations=Kalanki,Kathmandu
– sensor
● sensor=false
API Response
Application UI
Call on the
AsyncTask
on click
Do NOT FORGET to use
the INTERNET permission
● How do we implement this ?
– Get the JSONObject
– Extract Results from the API's response
– Putting it all together
Get the JSONObject
● Create a HTTPClient
– DefaultHttpClient httpClient = new DefaultHttpClient();
● Use the HTTPGet
– HttpGet httpGet = new HttpGet(url);
● Get the HTTPResponse
– HttpResponse httpResponse = httpClient.execute(httpGet);
● Get the HTTPEntity to an InputStream
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent(); // InputStream is
● Pass the InputStream to a BufferedReader
● Convert the output to a JSONObject
Extracting results from the API's
response
● To get the JSON output we use:
– JSONObject
● '{' represents the JSONObject
– JSONArray
● '[' represents the JSONArray
JSONObject
JSONArray
Accessing JSON
JSONObject googleObject1, googleObject2, googleObject3, googleObject4;
JSONArray googleArray1, googleArray2;
...
String distanceBetween = "";
try{
//Getting array of the API
UserFunctions users = new UserFunctions();
googleObject1 = users.distanceGET(places[0], places[1]);
//places[0] and places[1] are values passed on button click
googleArray1 = googleObject1.getJSONArray(TAG_ROW);
googleObject2 = googleArray1.getJSONObject(0);
googleArray2 = googleObject2.getJSONArray(TAG_ELEMENTS);
googleObject3 = googleArray2.getJSONObject(0);
googleObject4 = googleObject3.getJSONObject(TAG_DISTANCE);
distanceBetween = googleObject4.getString(TAG_TEXT);
}catch(JSONException e){
e.printStackTrace();
}
●
Putting it all together
● A separate class is created to convert the URL
response to a JSONObject
● Button click triggers the AsyncTask where inputs
for the doInBackground() are stated
● All the accessing/references to the API is done in
the AsyncTask, where the main logic lies in the
doInBackground()
●
doInBackground() returns results to the
onPostExecute() where it refreshes the UI thread
ListViews
● Displays a group of scrollable items
● The items are automatically inserted to list
using an Adapter that pull content from a
source
Implementation
● Create a layout with listview
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
In MainActivity
● Define the listview
listView = (ListView) findViewById(R.id.list);
● Define arrays to show in the ListView
String[] values = { “abc”, “def” , “ijk” , “xyz”};
● Use Adapter
– Helper to feed data into the list view
Using Adapter
● What are the parameters to be passed in this
adapter ?
– First parameter - Context
– Second parameter - Layout for the row
– Third parameter - ID of the TextView to which the
data is written
– Forth - the Array of data
● Define a new Adapter
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,android.R.layout.simple_list_
item_1, android.R.id.text1, values);
– These are all generic layouts defined by Android for us
● Set the adapter
– listView.setAdapter(adapter);
Notice android
being
referenced
at first
● Set onItemClickListener
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int itemPosition = position;
String itemValue = (String)
listView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue ,
Toast.LENGTH_LONG).show();
}
ListView Usage

More Related Content

ODP
Android training day 4
Vivek Bhusal
 
ODP
Day 6
Vivek Bhusal
 
ODP
Android training day 2
Vivek Bhusal
 
PDF
Day 5
Vivek Bhusal
 
ODP
Day seven
Vivek Bhusal
 
ODP
Session 2- day 3
Vivek Bhusal
 
PPTX
Angular2 + rxjs
Christoffer Noring
 
PDF
Advanced Dagger talk from 360andev
Mike Nakhimovich
 
Android training day 4
Vivek Bhusal
 
Android training day 2
Vivek Bhusal
 
Day seven
Vivek Bhusal
 
Session 2- day 3
Vivek Bhusal
 
Angular2 + rxjs
Christoffer Noring
 
Advanced Dagger talk from 360andev
Mike Nakhimovich
 

What's hot (20)

PDF
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
ODP
Android App Development - 04 Views and layouts
Diego Grancini
 
PPTX
React hooks
Assaf Gannon
 
PDF
Who killed object oriented design?
Amir Barylko
 
PDF
Deep Dive into React Hooks
Felix Kühl
 
PDF
Workshop 19: ReactJS Introduction
Visual Engineering
 
PPT
ExtJs Basic Part-1
Mindfire Solutions
 
PPTX
Angular Workshop_Sarajevo2
Christoffer Noring
 
PPT
jQuery Plugin
rbiggs
 
PPTX
Migrating an application from Angular 1 to Angular 2
Ross Dederer
 
PDF
Advanced javascript
Doeun KOCH
 
PDF
준비하세요 Angular js 2.0
Jeado Ko
 
PPTX
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
PPTX
React with Redux
Stanimir Todorov
 
PPTX
Introduction to react and redux
Cuong Ho
 
PDF
React + Redux. Best practices
Clickky
 
PDF
CDI: How do I ?
Antonio Goncalves
 
PPTX
Extend sdk
Harsha Nagaraj
 
PPTX
Developing New Widgets for your Views in Owl
Odoo
 
PDF
Speed up your GWT coding with gQuery
Manuel Carrasco Moñino
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Visual Engineering
 
Android App Development - 04 Views and layouts
Diego Grancini
 
React hooks
Assaf Gannon
 
Who killed object oriented design?
Amir Barylko
 
Deep Dive into React Hooks
Felix Kühl
 
Workshop 19: ReactJS Introduction
Visual Engineering
 
ExtJs Basic Part-1
Mindfire Solutions
 
Angular Workshop_Sarajevo2
Christoffer Noring
 
jQuery Plugin
rbiggs
 
Migrating an application from Angular 1 to Angular 2
Ross Dederer
 
Advanced javascript
Doeun KOCH
 
준비하세요 Angular js 2.0
Jeado Ko
 
Sword fighting with Dagger GDG-NYC Jan 2016
Mike Nakhimovich
 
React with Redux
Stanimir Todorov
 
Introduction to react and redux
Cuong Ho
 
React + Redux. Best practices
Clickky
 
CDI: How do I ?
Antonio Goncalves
 
Extend sdk
Harsha Nagaraj
 
Developing New Widgets for your Views in Owl
Odoo
 
Speed up your GWT coding with gQuery
Manuel Carrasco Moñino
 
Ad

Similar to Android training day 5 (20)

PDF
Android Best Practices
Yekmer Simsek
 
PDF
Android best practices
Jose Manuel Ortega Candel
 
PPT
Introducing Struts 2
wiradikusuma
 
PDF
Overview of Android Infrastructure
Alexey Buzdin
 
PDF
Overview of Android Infrastructure
C.T.Co
 
PPTX
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
Daniel Fisher
 
PDF
Taming Core Data by Arek Holko, Macoscope
Macoscope
 
ODP
ActiveWeb: Chicago Java User Group Presentation
ipolevoy
 
PDF
Session 9 Android Web Services - Part 2.pdf
EngmohammedAlzared
 
PDF
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
PDF
Android Pro Recipes
Gabriel Dogaru
 
PPTX
Android 3
Robert Cooper
 
KEY
CouchDB on Android
Sven Haiges
 
PPTX
Тарас Олексин - Sculpt! Your! Tests!
DataArt
 
PPTX
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
Ali Parmaksiz
 
PDF
Oleksandr Tolstykh
CodeFest
 
PPT
比XML更好用的Java Annotation
javatwo2011
 
PDF
From Legacy to Hexagonal (An Unexpected Android Journey)
Jose Manuel Pereira Garcia
 
PDF
Testable JavaScript: Application Architecture
Mark Trostler
 
PPT
GWT
guest245c88
 
Android Best Practices
Yekmer Simsek
 
Android best practices
Jose Manuel Ortega Candel
 
Introducing Struts 2
wiradikusuma
 
Overview of Android Infrastructure
Alexey Buzdin
 
Overview of Android Infrastructure
C.T.Co
 
2008 - TechDays PT: WCF, JSON and AJAX for performance and manageability
Daniel Fisher
 
Taming Core Data by Arek Holko, Macoscope
Macoscope
 
ActiveWeb: Chicago Java User Group Presentation
ipolevoy
 
Session 9 Android Web Services - Part 2.pdf
EngmohammedAlzared
 
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Android Pro Recipes
Gabriel Dogaru
 
Android 3
Robert Cooper
 
CouchDB on Android
Sven Haiges
 
Тарас Олексин - Sculpt! Your! Tests!
DataArt
 
A GWT Application with MVP Pattern Deploying to CloudFoundry using Spring Roo
Ali Parmaksiz
 
Oleksandr Tolstykh
CodeFest
 
比XML更好用的Java Annotation
javatwo2011
 
From Legacy to Hexagonal (An Unexpected Android Journey)
Jose Manuel Pereira Garcia
 
Testable JavaScript: Application Architecture
Mark Trostler
 
Ad

More from Vivek Bhusal (9)

PDF
Training Session 2 - Day 2
Vivek Bhusal
 
PDF
Training Session 2
Vivek Bhusal
 
PDF
Android training day 3
Vivek Bhusal
 
ODP
Android training day 1
Vivek Bhusal
 
PPTX
Stores munk presentation_aug10 (1)
Vivek Bhusal
 
PPTX
Mybudget
Vivek Bhusal
 
PDF
Wisevote - opendataweek @
Vivek Bhusal
 
PDF
Android training at GDG kathmandu Startup weekend bootcamp
Vivek Bhusal
 
PPTX
My medical info
Vivek Bhusal
 
Training Session 2 - Day 2
Vivek Bhusal
 
Training Session 2
Vivek Bhusal
 
Android training day 3
Vivek Bhusal
 
Android training day 1
Vivek Bhusal
 
Stores munk presentation_aug10 (1)
Vivek Bhusal
 
Mybudget
Vivek Bhusal
 
Wisevote - opendataweek @
Vivek Bhusal
 
Android training at GDG kathmandu Startup weekend bootcamp
Vivek Bhusal
 
My medical info
Vivek Bhusal
 

Recently uploaded (20)

PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Software Development Methodologies in 2025
KodekX
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 

Android training day 5

  • 1. Android Apps Development – Training Day 5
  • 2. Previously public void onClick(View arg0) { String name = ETname.getText().toString(); String email = ETemail.getText().toString(); String phoneno = ETphoneno.getText().toString(); if(name.length()<1 || email.length()<1 || phoneno.length()<1){ //do something }else{ dbhelper db= new dbhelper(getApplicationContext()); db.open(); Long response = db.insertdata(name, email, phoneno); db.close(); } }
  • 3. AsyncTask ● Perform operations on a different thread and also do this
  • 4. Adding to that public void onClick(View arg0) { String name = ETname.getText().toString(); String email = ETemail.getText().toString(); String phoneno = ETphoneno.getText().toString(); if(name.length()<1 || email.length()<1 || phoneno.length()<1){ //do something }else{ new DBAsync().execute(name, email, phoneno); } } This is the AsyncTask
  • 5. How do we do this ? ● We create a class inside our Activity that extends the AsyncTask ● We use different methods it offers do perform operations in the background: – OnPreExecute() ● Generally used to load the progress bar – doInBackground(Params... ) ● All the logic is dumped here – OnProgressUpdate() ● Called when publishProgress() is called in the doInBackground() – onPostExecute(Result) ● Gives out the desired results
  • 6. Class that extends the AsyncTask public class DBAsync extends AsyncTask <String, String, String>{ // code here } What are these?
  • 7. ● AsyncTask<Params, Progress, Result> – Params: the input, what you pass into the AsyncTask – Progress: on updates, passed to onProgressUpdate() – Result: the output from doInBackground() returned to the onPostExecute()
  • 8. onPreExecute @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); //Declare progress as a global variable progress = new ProgressDialog(Registration.this); progress.setProgressStyle(ProgressDialog.STYLE_SPINNER ); progress.setMessage("Filling up Database ..."); progress.show(); }
  • 9. doInBackground @Override protected String doInBackground(String... string) { // TODO Auto-generated method stub dbhelper db = new dbhelper(getApplicationContext()); db.open(); Long insert = db.insertdata(string[0], string[1], string[2]); db.close(); return null; }
  • 10. onPostExecute @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); progress.dismiss(); Intent i = new Intent(Registration.this, afterlogin.class); i.putExtra("register", true); startActivity(i); }
  • 11. Deeper with AsyncTask ● The first think you should understand: – They rely on Java concepts of GENERICS and VARARGS GENERICS: ● Can declare of any type VARARGS: ● Arrays… you can pass in number of values
  • 12. Accessing HTTP ● There are two ways of accessing HTTP in android: – Using the HttpURLConnection ● Using the HttpURLConnection object and its method – Using the HttpClient ● Use DefaultHttpClient, Http GET & POST, HttpResponse, HttpEntity ● Both of these follow similar steps: – Process the Http input – Use InputStream and BufferedReader to iterate through the http output.
  • 13. DistanceMatrix API ● URL – http://maps.googleapis.com/maps/api/distancematrix/json? params ● Parameters: – origins ● origins=Jamal,Kathmandu – destinations ● destinations=Kalanki,Kathmandu – sensor ● sensor=false
  • 15. Application UI Call on the AsyncTask on click Do NOT FORGET to use the INTERNET permission
  • 16. ● How do we implement this ? – Get the JSONObject – Extract Results from the API's response – Putting it all together
  • 17. Get the JSONObject ● Create a HTTPClient – DefaultHttpClient httpClient = new DefaultHttpClient(); ● Use the HTTPGet – HttpGet httpGet = new HttpGet(url); ● Get the HTTPResponse – HttpResponse httpResponse = httpClient.execute(httpGet); ● Get the HTTPEntity to an InputStream HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); // InputStream is ● Pass the InputStream to a BufferedReader ● Convert the output to a JSONObject
  • 18. Extracting results from the API's response ● To get the JSON output we use: – JSONObject ● '{' represents the JSONObject – JSONArray ● '[' represents the JSONArray
  • 20. Accessing JSON JSONObject googleObject1, googleObject2, googleObject3, googleObject4; JSONArray googleArray1, googleArray2; ... String distanceBetween = ""; try{ //Getting array of the API UserFunctions users = new UserFunctions(); googleObject1 = users.distanceGET(places[0], places[1]); //places[0] and places[1] are values passed on button click googleArray1 = googleObject1.getJSONArray(TAG_ROW); googleObject2 = googleArray1.getJSONObject(0); googleArray2 = googleObject2.getJSONArray(TAG_ELEMENTS); googleObject3 = googleArray2.getJSONObject(0); googleObject4 = googleObject3.getJSONObject(TAG_DISTANCE); distanceBetween = googleObject4.getString(TAG_TEXT); }catch(JSONException e){ e.printStackTrace(); } ●
  • 21. Putting it all together ● A separate class is created to convert the URL response to a JSONObject ● Button click triggers the AsyncTask where inputs for the doInBackground() are stated ● All the accessing/references to the API is done in the AsyncTask, where the main logic lies in the doInBackground() ● doInBackground() returns results to the onPostExecute() where it refreshes the UI thread
  • 22. ListViews ● Displays a group of scrollable items ● The items are automatically inserted to list using an Adapter that pull content from a source
  • 23. Implementation ● Create a layout with listview <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_height="wrap_content" android:layout_width="match_parent"> </ListView> </LinearLayout>
  • 24. In MainActivity ● Define the listview listView = (ListView) findViewById(R.id.list); ● Define arrays to show in the ListView String[] values = { “abc”, “def” , “ijk” , “xyz”}; ● Use Adapter – Helper to feed data into the list view
  • 25. Using Adapter ● What are the parameters to be passed in this adapter ? – First parameter - Context – Second parameter - Layout for the row – Third parameter - ID of the TextView to which the data is written – Forth - the Array of data
  • 26. ● Define a new Adapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_ item_1, android.R.id.text1, values); – These are all generic layouts defined by Android for us ● Set the adapter – listView.setAdapter(adapter); Notice android being referenced at first
  • 27. ● Set onItemClickListener listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int itemPosition = position; String itemValue = (String) listView.getItemAtPosition(position); Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show(); }