Breaking

Friday, 30 November 2018

Adapter Tutorial With Example In Android Studio | Android Solution | Adapter Tutoriyal


Adapter Tutorial With Example In Android Studio

In Android, Adapter is an extension between UI part and information source that encourages us to fill information in UI segment. It holds the information and send the information to an Adapter see at that point view can takes the information from the adapter view and demonstrates the information on various perspectives like as ListView, GridView, Spinner and so forth. For more customization in Views we utilizes the base adapter or custom adapters.

To fill information in a rundown or a framework we have to actualize Adapter. Adapters acts like an extension between UI part and information source. Here information source is the source from where we get the information and UI segments are rundown or matrix things in which we need to show that information.

Adapters In Android:


There are the some normally utilized Adapter in Android used to fill the information in the UI parts.
BaseAdapter – It is parent adapter for every single other adapter

ArrayAdapter – It is utilized at whatever point we have a rundown of single things which is upheld by a cluster

Custom ArrayAdapter – It is utilized at whatever point we have to show a custom rundown

SimpleAdapter – It is a simple adapter to delineate information to sees characterized in your XML document

Custom SimpleAdapter – It is utilized at whatever point we have to show a customized rundown and expected to get to the youngster things of the rundown or matrix

Presently we portray every adapter one by one in detail:

1. BaseAdapter In Android:


BaseAdapter is a typical base class of a general execution of an Adapter that can be utilized in ListView, GridView, Spinner and so forth. At whatever point we require a customized rundown in a ListView or customized lattices in a GridView we make our very own adapter and expand base adapter in that. Base Adapter can be stretched out to make a custom Adapter for showing a custom rundown thing. ArrayAdapter is additionally an execution of BaseAdapter.

Custom Adapter code which broadens the BaseAdapter in that:


public class CustomAdapter broadens BaseAdapter {
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int I) {
return invalid;
}
@Override
public long getItemId(int I) {
return 0;
}
@Override
public View getView(int I, View see, viewGroup) {
return invalid;
}


In above code bit we see the overrided elements of BaseAdapter which are utilized to set the information in a rundown, lattice or a spinner. These capacities are depicted in BaseAdapter tutorial with model.

2. ArrayAdapter In Android:


At whatever point we have a rundown of single things which is supported by an Array, we can utilize ArrayAdapter. For example, rundown of telephone contacts, nations or names.

Here is the means by which android ArrayAdapter looks ::

ArrayAdapter(Context setting, int asset, int textViewResourceId, T[] objects)

The above capacity are portrayed in ArrayAdapter tutorial with precedent.

3. Custom ArrayAdapter In Android:


ArrayAdapter is additionally an execution of BaseAdapter, so on the off chance that we need more customization, we can make a custom adapter and broaden ArrayAdapter in that. Since cluster adapter is a usage of BaseAdapter, so we can supersede all the capacity's of BaseAdapter in our custom adapter.

Beneath Custom adapter class MyAdapter broadens ArrayAdapter in that:


public class MyAdapter broadens ArrayAdapter {
public MyAdapter(Context setting, int asset, int textViewResourceId, List objects) {
super(context, asset, textViewResourceId, objects);
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
}


These capacities are portrayed in Custom ArrayAdapter tutorial with model.

4. SimpleAdapter In Android:


In Android SimpleAdapter is a simple Adapter to outline information to sees characterized in a XML file(layout). In Android we can indicate the information support to a rundown as an ArrayList of Maps(i.e. hashmap or other). Every passage in an ArrayList is comparing to one column of a rundown.

The Map contains the information for each line. Here we additionally indicate a XML file(custom list things record) that characterizes the perspectives which is utilized to show the line, and a mapping from keys in the Map to explicit perspectives.

At whatever point we need to make a custom rundown we have to execute custom adapter. As we talk about prior ArrayAdapter is utilized when we have a rundown of single thing's sponsored by an Array. So in the event that we require more customization in a ListView or a GridView we have to execute basic adapter.

SimpleAdapter code in Android:

SimpleAdapter (Context setting, List<? broadens Map<String, ?>> information, int asset, String[] from, int[] to)

The above parameters of a basic Adapter is portrayed in SimpleAdapter tutorial with model.

5. Custom SimpleAdapter In Android:


At whatever point we need to make a custom rundown we have to execute custom adapter. As we talk about prior ArrayAdapter is utilized when we have a rundown of single thing's upheld by an Array. So in the event that we require customization in a ListView or a GridView we have to execute straightforward Adapter yet when we require more customization in rundown or network things where we have many view's in a rundown thing and afterward we need to play out any occasion like snap or some other occasion to a specific view then we have to actualize a custom adapter who satisfies our necessity's and very simple to be actualized.

BaseAdapter is the parent adapter for every single other adapter so on the off chance that we expands a SimpleAdapter, we can likewise abrogate the base adapter's capacity in that class.
Essential Note: We can't perform occasions like snap and other occasion on tyke thing of a rundown or matrix however on the off chance that we have a few prerequisites to do that, we can make our very own custom adapter and expands the basic adapter in that.

Custom Adapter expands SimpleAdapter in that:


public class CustomAdapter expands SimpleAdapter {
public CustomAdapter(Context setting, List<? expands Map<String, ?>> information, int asset, String[] from, int[] to) {
super(context, information, asset, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public int getCount() {
return super.getCount();
}
}


The above overrided elements of basic adapter are now depicted in Custom SimpleAdapter article.

Adapter Example In Android Studio:


The following is the Android Studio model which demonstrate the utilization of the Adapter in Android. In this model we show a rundown of organic products names with pictures by utilizing SimpleAdapter and at whatever point client tap on a rundown thing the natural product's name showed in a Toast.

Step 1: Create another undertaking and name it SimpleAdapterExample.

Step 2: Open res - > design - > xml (or) main.xml and include following code :

In this step we open a xml record and include the code for showing a ListView by utilizing its distinctive qualities.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/apparatuses"
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: Save natural product pictures in drawable envelope with name apple, banana, litchi, mango and pineapple.

Step 4: Open src - > bundle - > MainActivity.java
In this step we include the code for start ListView and set the information in the rundown. In this right off the bat we make two exhibits first for natural product names and second for organic products pictures and after that set the information in the ListView utilizing SimpleAdapter.


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;
public class MainActivity expands AppCompatActivity {
/introduce view's
ListView simpleListView;
String[] fruitsNames = {"Apple", "Banana", "Litchi", "Mango", "PineApple"};//natural product names exhibit
int[] fruitsImages = {R.drawable.apple, R.drawable.banana, R.drawable.litchi, R.drawable.mango, R.drawable.pineapple};//natural products pictures
@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<fruitsNames.length;i++)
{
HashMap<String,String> hashMap=new HashMap<>();//make a hashmap to store the information in key esteem match
hashMap.put("name",fruitsNames[i]);
hashMap.put("image",fruitsImages[i]+"");
arrayList.add(hashMap);//include the hashmap into arrayList
}
String[] from={"name","image"};//string exhibit
int[] to={R.id.textView,R.id.imageView};//int exhibit of perspectives id's
SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.list_view_items,from,to);//Create question and set the parameters for simpleAdapter
simpleListView.setAdapter(simpleAdapter);//sets the adapter for listView
/perform listView thing click occasion
simpleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View see, int I, long l) {
Toast.makeText(getApplicationContext(),fruitsNames[i],Toast.LENGTH_LONG).show();//demonstrate the chose picture in toast as indicated by position
}
});
}
}


Step 5: Create new format > rec-> design > list_view_items.xml and include following code:
In this step we make a xml document for showing ListView things. In this xml we include the code for showing an ImageView and a TextView.


<?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"
android:background="#fff">
<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>




No comments:

Post a Comment