Custom List View with Images and text using BaseAdapter.

     In this tutorial we are implement the customListView using BaseAdapter. In CustomListView show the ImageView with TextView for each list item. 
As the easy ListView, custom ListView conjointly uses Adapter categories that additional the content from knowledge supply (such as string array, array, database etc). Adapter bridges knowledge between associate degree AdapterViews and other Views.
A list read is an adapter read that doesn't understand the small print, like sort and contents, of the views it contains. Instead list read requests views on demand from a ListAdapter as needed, like to show new views because the user scrolls up or down.

In order to show things within the list, decision setAdapter(ListAdapter) to associate associate degree adapter with the list. For an easy example, see the discussion of filling an adapter read with text within the Layouts guide.

To show a additional custom read for every item in your dataset, implement a ListAdapter. for instance, extend BaseAdapter and build and configure the read for every knowledge item in getView(...):
ListView tries to utilize read objects so as to boost performance and avoid a lag in response to user scrolls. to require advantage of this feature, check if the convertView provided to getView(...) is null before making or inflating a brand new read object. See creating ListView Scrolling sleek for additional ways that to confirm a sleek user expertise.

For a additional complete example of making a custom adapter, see the Custom selection List sample app.

To specify associate degree action once a user clicks or faucets on one list item, see Handling click events.

To learn a way to populate an inventory read with a CursorAdapter, see the discussion of filling associate degree adapter read with text within the Layouts guide. See using a Loader to be told a way to avoid interference the most thread once employing a indicator.


Note, several examples use ListActivity or ListFragment to show an inventory read. Instead, favor the additional versatile approach once writing your own app: use a additional generic Activity taxon or Fragment taxon and add an inventory read to the layout or read hierarchy directly. This approach offers you additional direct management of the list read and adapter.

Follow below steps:


-in main_activity.xml add List View widgets main_activity.xml


 <?xml version="1.0" encoding="utf-8"?><  
 LinearLayout 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"  
   android:orientation="vertical"  
   android:padding="10dp"  
   tools:context="com.example.aaru.customizelistview.MainActivity">  

   <ListView  android:layout_width="match_parent"   
   android:layout_height="match_parent"  
   android:id="@+id/listView"  >  
   </ListView>   

  </LinearLayout>  



-In this Part get List View id in MainActivity.java  and put dummy images in drawble folder. 
MainActivity.java


 package com.example.aaru.customizelistview;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ListView;  
 import android.widget.Toast;  
 import java.util.ArrayList;  
 public class MainActivity extends AppCompatActivity {  
 ListView listView;  
   ArrayList<String> alName=new ArrayList<>();    
  ArrayList<Integer> alImage=new ArrayList<>();  
   @Override  protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);     
     setContentView(R.layout.activity_main);  
     listView= (ListView) findViewById(R.id.listView);  
     for(int i=0;i<8;i++)  
     {  
       alName.add("Name "+(i+1));  
     }  
     alImage.add(R.drawable.sample_0);      
     alImage.add(R.drawable.sample_1);     
     alImage.add(R.drawable.sample_2);   
     alImage.add(R.drawable.sample_3);  
     alImage.add(R.drawable.sample_4);  
     alImage.add(R.drawable.sample_5);  
     alImage.add(R.drawable.sample_6);  
     alImage.add(R.drawable.sample_7);  
     CustomAdapter adapter=new CustomAdapter(alName,alImage,MainActivity.this);   
      listView.setAdapter(adapter);  
    //below is row item click not a item click lisner  
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
       @Override   
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
     Toast.makeText(MainActivity.this, alName.get(position), Toast.LENGTH_SHORT).show();  
       }  
     });  
   }  
 }  


-Create Extra layout file name is raw right click on project package >new>xml>layoout

raw.xml



 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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="wrap_content"  
   android:orientation="horizontal"  
   android:padding="10dp"  
   tools:context="com.example.aaru.customizelistview.MainActivity">  
   <ImageView   
     android:id="@+id/imageView"  
     android:layout_width="100dp"  
     android:layout_height="100dp"  
     app:srcCompat="@mipmap/ic_launcher" />  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:orientation="vertical">  
     <TextView  
       android:id="@+id/txtName"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_weight="1"  
       android:text="TextView"  
       android:layout_gravity="center"  
       android:textSize="25sp" />  
   </LinearLayout></LinearLayout>  

-Create extra class  name is Custom adapter extend with base adapter . CustomAdapter.java

 package com.example.aaru.customizelistview;  
 import android.content.Context;  
 import android.content.Intent;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 import java.util.ArrayList;  
 /** * Created by Aaru on 9/27/2017. */  
 public class CustomAdapter extends BaseAdapter{  
   ArrayList<String> alName=new ArrayList<>();  
   ArrayList<Integer> alImage=new ArrayList<>();  
   Context cotext;  LayoutInflater inflator;  
  public CustomAdapter(ArrayList<String> alName, ArrayList<Integer> alImage, Context cotext)  
   {  
     this.alImage=alImage;  
     this.alName=alName;  
     this.cotext=cotext;  
     inflator=(LayoutInflater)cotext.getSystemService(cotext.LAYOUT_INFLATER_SERVICE);  
   }  
   @Override  
   public int getCount() {  
    //alNam.size() is print raw equal to alName Size.  
     return alName.size();  
   }  
   @Override  
   public Object getItem(int position) {  
     return null;  
   }  
   @Override  
   public long getItemId(int position) {  
     return 0;  
   }  
   @Override  
   public View getView(final int position, final View convertView, final ViewGroup parent)  
   {  
     final View view=inflator.inflate(R.layout.raw,null);  
     TextView txtName=(TextView)view.findViewById(R.id.txtName);  
     ImageView image=(ImageView)view.findViewById(R.id.imageView);  
     image.setImageResource(alImage.get(position));  
     txtName.setText(alName.get(position));  
     image.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
     //this is onItem Click Lisner when user is click on  
     // Image we post the the data to another activity  
         Intent i=new Intent(cotext,Main2Activity.class);  
         i.putExtra("image",alImage.get(position));  
         cotext.startActivity(i);  
       }  
     });  
     return view;  
   }  
 }  
>>>>>>>>>>>>>>>CustomListview is done>>>>>>>>>>>>>>

Important Part !!

listview setup is complete than setup the on item click and raw click .

rawclick is already done is MainActivity.class.

so we going to itemclick let's do it

>>You show item click listener in CustomAdpater.class 
>>we put item click listener in image and get the image position when user is click on    image    and pass  the    Image Data another activity using Intent. lets create a one Extra activity name is  Main2Activity   for getting image.




activity_main2.xml




 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout 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"  
   android:orientation="vertical"  
   tools:context="com.example.aaru.customizelistview.Main2Activity">  
   <ImageView  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:id="@+id/imageView"   
     />  
 </LinearLayout>  





Main2Activity.class




 package com.example.aaru.customizelistview;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.widget.ImageView;  
 public class Main2Activity extends AppCompatActivity {  
   ImageView imageView;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main2);  
     imageView=(ImageView)findViewById(R.id.imageView);  
     int i=getIntent().getIntExtra("image",0);  
     imageView.setImageResource(i);  
   }  
 }  








>>>>>>>>>>>>.Enjoy Coding>>>>>>>>>>>>>>>>



Comments