Wednesday 18 October 2017

9. Using Custom Adapter in Android Application

9. Using Custom Adapter in Android Application




Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.

ADAPTER:  bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can takes the data from adapter view and shows the data on different views like as spinner, list view, grid view etc.

The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.

For custom Adapter, Lets create Animal list, where we will store animal name and animal image details for each item. We will need Animal POJO for the same.

CustomListAdapter:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="tutorial.android.sachin4droid.androidtutorials.CustomListActivity"
  >

  <ListView
      android:id="@+id/customListActivityListView"
      android:layout_width="match_parent"
      android:layout_height="400dp"
      android:dividerHeight="5dp"
      android:divider="@android:color/holo_blue_bright">

  </ListView>


</RelativeLayout>

Animal:
package tutorial.android.sachin4droid.androidtutorials.bean;

/**
* Created by sachin on 17/10/17.
*/

public class Animal {

  private String animalName;
  private String animalImage;

  public Animal(){}

  public Animal(String animalName,String animalImage){
      this.animalName=animalName;
      this.animalImage=animalImage;

  }

  public String getAnimalName() {
      return animalName;
  }

  public void setAnimalName(String animalName) {
      this.animalName = animalName;
  }

  public String getAnimalImage() {
      return animalImage;
  }

  public void setAnimalImage(String animalImage) {
      this.animalImage = animalImage;
  }
}
   

CustomListActivity:
package tutorial.android.sachin4droid.androidtutorials;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import tutorial.android.sachin4droid.androidtutorials.adapter.CustomListAdapter;
import tutorial.android.sachin4droid.androidtutorials.bean.Animal;

public class CustomListActivity extends AppCompatActivity {

  ListView customListActivityListView;
  List<Animal> animals = new ArrayList<>();

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

      customListActivityListView = (ListView) findViewById(R.id.customListActivityListView);

      animals =  createAnimalList();
      CustomListAdapter customListAdapter = new CustomListAdapter(this, R.layout.custom_list_item,animals);
      customListActivityListView.setAdapter(customListAdapter);

  }

  private List<Animal> createAnimalList() {
      List<Animal> animalList = new ArrayList<>();

      Animal cat = new Animal("cat", "cat.jpg");
      Animal dog = new Animal("dog", "dog.jpg");
      Animal monkey = new Animal("monkey", "monkey.jpg");
      Animal tiger = new Animal("tiger", "tiger.jpg");

      animalList.add(cat);
      animalList.add(dog);
      animalList.add(monkey);
      animalList.add(tiger);

      return animalList;



  }
}

MainActivity:

if("CustomListActivity"== activityList.get(i) ){
  Intent intent = new Intent(MainActivity.this, CustomListActivity.class);
  startActivity(intent);

}

Assets folder images:



Activity_custom_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="tutorial.android.sachin4droid.androidtutorials.CustomListActivity"
  >

  <ListView
      android:id="@+id/customListActivityListView"
      android:layout_width="match_parent"
      android:layout_height="400dp"
      android:dividerHeight="5dp"
      android:divider="@android:color/holo_blue_bright">

  </ListView>


</RelativeLayout>


Custom_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:weightSum="2">

  <ImageView
      android:id="@+id/animalmageView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      />
  <TextView
      android:id="@+id/animalNameTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="TextView" />

</LinearLayout>



That's all about custom adapters. Thank you.


No comments:

Post a Comment

Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...