SimpleAdapter Tutorial With Examples In Android Studio
In android,
A connector is an extension between UI component and information source that
causes us to fill information in UI component. SimpleAdpater is used for
customization of rundown or matrix things.
In Android,
SimpleAdapter is a simple connector to delineate information to sees
characterized in a XML record (layout). In android you can indicate the
information support to a rundown as an ArrayList of Maps(hashmap or other).
Every passage in an ArrayList is relating to one line of a rundown. The Map
contains the information for each column. You additionally indicate a XML
file(custom list things record) that characterizes the perspectives used to
show the column, and a mapping from keys in the Map to explicit perspectives.
The subject of will bode well when you complete one precedent in Android Studio
as examined toward the finish of this article.
Whenever we
need to make a custom rundown we have to execute custom connector. As we
examine prior ArrayAdapter is used when we have a rundown of single thing's
sponsored by an Array. So on the off chance that we require more customization
in a ListView or a GridView we have to execute straightforward connector.
The following is android SimpleAdapter code:
SimpleAdapter(Context
context, List<? expands Map<String, ?>> information, int resource,
String[] from, int[] to)
In above
code piece we demonstrate the execution of a SimpleAdapter. The following is
the depiction of the considerable number of parameters used for the usage of a
SimpleAdapter to demonstrate a rundown of components in a rundown see or in a
matrix see.
Parameters
used in SimpleAdapter:
1.
context:
The
principal parameter is used to pass the context implies the reference of
current class. Here this is a watchword used to demonstrate the current class
reference. We can likewise utilize getApplicationContext(), getActivity() in
the place of this watchword. getApplicationContext() is used in an Activity and
getActivity() is used in a Fragment.
The
following is the model code in which we set the current class reference in a
connector.
SimpleAdapter(this, List<? broadens
Map<String, ?>> information, int resource, String[] from, int[] to)
|
2. information:
The second
parameter is information which is a List of Maps. Every passage in a List
relates to one line in the rundown. The Maps contains the information of each
line and ought to incorporate every one of the sections indicated in
"from" string exhibit.
The
following is the model, In which we set's the guide type arrayList.
SimpleAdapter(this, hashmapData, int
resource, String[] from, int[] to)
|
3. resource:
The third
parameter is resource id which is use to set the layout (xml document) for rundown
things in which you have a content view, picture see or some other view.
The
following is the precedent code in which we set the layout (xml record) for
making custom rundown.
Assume
underneath is the code of Activity custom_list_items.xml where we characterize
ImageView and TextView as rundown things which will be set in Layout:
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:text="Demo"
android:textColor="#000"/>
</RelativeLayout>
|
Presently we
will pass this layout as third parameter in SimpleAdapter.
SimpleAdapter (this, hashmapData,
R.layout.custom_list_items, String[] from, int[] to)
|
4. from:
The fourth
parameter is from is a string cluster or called a rundown of segment names that
will be added to a Map related with every thing of a network or rundown see.
The
following is the model code in which we set a cluster for rundown things
String
fromArray[]={"userName","userImage"};
SimpleAdapter (this, hashmapData,
R.layout.custom_list_items, fromArray, int[] to)
|
5. to:
The fifth
and last parameter is to which is an integer exhibit used to store the Id's of
the perspectives. The perspectives that should show segment in the
"from" parameter. These should all be TextViews. The main N sees in
this rundown are given the estimations of the principal N sections in the
"from" parameter.
The
following is the precedent code, In which we set the integer cluster for
perspectives id's.
int to[]={R.id.textView,R.id.imageView};
SimpleAdapter (this, hashmapData,
R.layout.custom_list_items, fromArray,to)
|
Example of Simple Adapter in Android Studio: |
In
underneath case of straightforward connector we show the custom rundown of
creature names with their pictures in a rundown see. Here creature picture is
shown in the correct side of the screen and the name is shown to one side of
the screen. When you tap on any rundown thing the creature name from that thing
will be shown as a Toast to screen. The following is the last yield and code:
Step 1:
Create another venture in Android Studio and name it SimpleAdapterExample.
Select File
- > New - > New Project and Fill the structures and snap
"Complete" catch.
Step 2: Now
go to res - > layout - > xml (or) activity_main.xml and include following
code. Here we are structuring ListView in Relative Layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/devices"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"
android:listSelector="#600"/>
</RelativeLayout>
|
Step 3: Now
Create another Activity. Go to res-> right tap on layout-> new - >
Activity - > Blank Activity and make list_view_items.xml activity. Here
include following code. Here we are characterizing ImageView and TextView
things that will be shown inside each column of ListView.
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:text="Demo"
android:textColor="#000"/>
</RelativeLayout>
|
Step 4: Now
open application - > java - > package, tap on MainActivity.java and
include the beneath code. Here we will utilize SimpleAdapter to show static
information in XML layout. More clarification is incorporated into the code
itself.
package
example.abhiandriod.simpleadapterexample;
import
android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
open class MainActivity expands
AppCompatActivity {
/introduce view's
ListView simpleListView;
String[]
animalName={"Lion","Tiger","Monkey","Dog","Cat","Elephant"};//creature
names cluster
int[]
animalImages={R.drawable.lion,R.drawable.tiger,R.drawable.monkey,R.drawable.dog,R.drawable.cat,R.drawable.elephant};//creature
pictures cluster
@Override
secured void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleListView=(ListView)findViewById(R.id.simpleListView);
ArrayList<HashMap<String,String>>
arrayList=new ArrayList<>();
for (int i=0;i<animalName.length;i++)
{
HashMap<String,String> hashMap=new
HashMap<>();//make a hashmap to store the information in key esteem
combine
hashMap.put("name",animalName[i]);
hashMap.put("image",animalImages[i]+"");
arrayList.add(hashMap);//include the
hashmap into arrayList
}
String[]
from={"name","image"};//string cluster
int[]
to={R.id.textView,R.id.imageView};//int cluster of views id's
SimpleAdapter simpleAdapter=new
SimpleAdapter(this,arrayList,R.layout.list_view_items,from,to);//Create protest
and set the parameters for simpleAdapter
simpleListView.setAdapter(simpleAdapter);//sets
the connector for listView
/perform listView thing click occasion
simpleListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
open void
onItemClick(AdapterView<?> adapterView, view, int I, long l) {
Toast.makeText(getApplicationContext(),animalName[i],Toast.LENGTH_LONG).show();//demonstrate
the chose picture in toast as per position
}
});
}
}
|
Output:
Presently
run the App in AVD or Emulator and you will see the ListView showing creature
names and pictures. Presently tap on the any Animal and you will see Toast
message indicating Animal name.
No comments:
Post a Comment