Skip to main content

ListView Using CustomAdapter with setter getter method

For this we need following three classes

  1. Main Class where we would be implementing listview using custom Adapter(CustomAdapterExample.java)
  2. Second Class for designing our Custom Adapter(CustomAdapter.java)
  3. And the last one for getter setter method for data transfer(User.java)

Along with these three java classes we need two xml file one would be default xml file (activity_custom_adapter_example.xml) for main class and another one for making Listview layout (simple_list_items.xml)

 In activity_custom_adapter_example.xml, take a ListView element from the Widgets and in simple_list_items.xml, take two textviews.


Here Goes the Complete Code for the same:

CustomAdapterExample.java

package com.AndroidDevelopmentGuru.customadapter_second;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;

import android.widget.ListView;

public class CustomAdapterExample extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_adapter_example);
 
    //Construct the data source
        ArrayList<User> arrayofUsers = new ArrayList<User>();
     
        arrayofUsers.add(new User("Anita", "987456578"));
        arrayofUsers.add(new User("Rahul", "897456579"));
        arrayofUsers.add(new User("Harminder", "987456578"));
        arrayofUsers.add(new User("Manisha", "987456578"));
        arrayofUsers.add(new User("Varun", "45565798"));
        arrayofUsers.add(new User("Manish", "987456578"));
     
  // Create the adapter to convert the array to views
        CustomAdapter adapter = new CustomAdapter(CustomAdapterExample.this, arrayofUsers);
   //Attach the adapter to Listview
        ListView list = (ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
 
     
    }

 
}

CustomAdapter.java

package com.AndroidDevelopmentGuru.customadapter_second;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

View view;
ArrayList<User> arraylist;

public CustomAdapter(CustomAdapterExample customAdapterExample,
ArrayList<User> arrayofUsers) {
// TODO Auto-generated constructor stub

arraylist=arrayofUsers;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}

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

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

@Override
public View getView(int positon, View view, ViewGroup parent) {
// TODO Auto-generated method stub

LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.simple_list_items, parent,false);

final User users = arraylist.get(positon);

TextView name = (TextView)view.findViewById(R.id.textView1);
name.setText(users.getName());

TextView contact =(TextView)view.findViewById(R.id.textView2);
contact.setText(users.getContactno());


return view;
}


}


User.java

package com.AndroidDevelopmentGuru.customadapter_second;

public class User {

private String name;
private String Contactno;

public User(String name, String contactno) {
// TODO Auto-generated constructor stub
this.name=name;
this.Contactno=contactno;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactno() {
return Contactno;
}
public void setContactno(String contactno) {
Contactno = contactno;
}

}


activity_custom_adapter_example.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=".CustomAdapterExample" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


simple_list_items.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Download This Code Here


OutPut:


Comments

Popular posts from this blog

Android: Login Screen using Fragments

In this tutorial, our focus is on making a User/Member login activity using fragments. With the help of Fragments we will use the same activity to show User login area and also Members Login area. Prerequisite for this tutorial: You should be know how to make an Activity And most importantly you should have prior Knowledge of Fragments. For practising basic Fragment implementation refer to Android Simple Fragment Example      We have used only three activities for this: MainActivity(which represents the Login Screen) Fragments for Members area Fragment for New Users

Simple Login/Register Example using SQLite database

MainActivity.java(login Screen) package com.AndroidDevelopmentGuru.database_new; import java.util.List; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity {                                 EditText user, pass;                 Button login, not_reg;                 DatabaseHandler db;           ...

Android: How to Generate Key Hash for Facebook Integration

Hello everyone, As this is a quite confusing for everyone to generate the key hash in your PC, so i decided to write about it. I have also tried many ways but every time i failed to generate the key hash. After trying for an hour i successfully generated the key. Just follow the below given Steps and you are done with this. Step I: Download Openssl from   here Step II: Download openssl-0.9.8e_X64.zip for 64 bit PC and openssl-0.9.8e_WIN32.zip for 32 bit PC Step III: once your download the zip folder. Go to C drive(where window is installed) and make a folder named openssl Step IV: extract all zip file in this folder. Step V: Now go to Java folder and copy path of jre. For example: C:\Program Files\Java\jre1.8.0_101\bin