Custom listview with base adapter


Step 1:- Create Main Activity, which has listview


package com.example.customlistviewstudio;

import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {

 private ListView listview;
 private ArrayListmodel arraylist;
 private StudioAdapter studioAdapter;

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

 }

 private void initialize() {

  listview = (ListView) findViewById(R.id.listView1);
  arraylist = new ArrayListmodel();
  model objmodel;

  for (int i = 0; i<10; i++) {
   objmodel = new model();
   objmodel.setTitle("Title:" + i);
   objmodel.setLike(i);
   objmodel.setShare(i);
   objmodel.setComment(i);
   arraylist.add(objmodel);

  }

  studioAdapter = new StudioAdapter(MainActivity.this, arraylist);
  listview.setAdapter(studioAdapter);

 }
}




activity_main.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.customlistviewstudio.MainActivity">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </ListView>

</RelativeLayout>


       
 

Step 2:- Create a model class which has getter and setter methods.


Model.java

package com.example.customlistviewstudio;
public class model {
 private String Title;
 private int like;
 private int share;
 private int comment;
 public String getTitle() {
  return Title;
 }
 public void setTitle(String title) {
  Title = title;
 }
 public int getLike() {
  return like;
 }
 public void setLike(int like) {
  this.like = like;
 }
 public int getShare() {
  return share;
 }
 public void setShare(int share) {
  this.share = share;
 }
 public int getComment() {
  return comment;
 }
 public void setComment(int comment) {
  this.comment = comment;
 }
}

       
 

step 3 :- Create custom adapter and pass arraylist into it.

StudioAdapter.java


package com.example.customlistviewstudio;

import java.util.ArrayList;

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 com.example.customlistviewstudio.model;
public class StudioAdapter extends BaseAdapter{

 private MainActivity mainactivity;
 private ArrayList arraylist;
 
 
 public StudioAdapter(MainActivity mainactivity,ArrayList arraylist)
 {
  this.arraylist=arraylist;
  this.mainactivity=mainactivity;
 }
 
 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return arraylist.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  
  ViewHolder holder=null;
  
  if(convertView==null){
   
   convertView=LayoutInflater.from(mainactivity).inflate(R.layout.row_studio, null);
   
   holder=new ViewHolder();
   holder.titel=(TextView) convertView.findViewById(R.id.Titel);
   holder.like = (TextView) convertView.findViewById(R.id.Like);
   holder.share = (TextView) convertView.findViewById(R.id.Share);
   // holder.comment =(TextView) convertView.findViewById(R.id.txt4);
   holder.img1 = (ImageView) convertView.findViewById(R.id.img1);
   
   convertView.setTag(holder);
  }
  else
  {
   holder=(ViewHolder)convertView.getTag();
  }
   // ArrayList arraylistPlaylist = null;
    //final model model = arraylistPlaylist.get(position);
    holder.titel.setText(arraylist.get(position).getTitle());
    holder.like.setText( ""+arraylist.get(position).getLike());
    holder.share.setText(""+arraylist.get(position).getShare());
    // holder.comment.setText(model.getComment());
    holder.img1.setImageResource(R.drawable.ic_launcher);
    return convertView;
 }
 class ViewHolder {
  public TextView titel;
  public TextView like;
  public TextView share;
  public ImageView img1;
 }
}
       
 


step 4 :- Below is layout which is inflated into listview row in above adapter.

row_studio.xml



 <?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:orientation="vertical" >

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"/>

<TextView
    android:id="@+id/Titel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Titel" />

<TextView
    android:id="@+id/Like"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/img1"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/img1"
    android:text="Like" />

<TextView
    android:id="@+id/Share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/Like"
    android:layout_alignLeft="@+id/Titel"
    android:text="Share" />

<TextView
    android:id="@+id/Comment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/Share"
    android:layout_alignParentRight="true"
    android:layout_marginRight="22dp"
    android:text="Comment" />


</RelativeLayout>

       
 

Comments

Most viewed