Skip to content

Instantly share code, notes, and snippets.

@menuka94
Created February 11, 2017 13:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save menuka94/7350536631c9fcffb1bce31daffff3ba to your computer and use it in GitHub Desktop.
Save menuka94/7350536631c9fcffb1bce31daffff3ba to your computer and use it in GitHub Desktop.
Android Custom List View with Custom Adapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.menuka.adaptertest.MainActivity">
<ListView
android:id="@+id/names_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
package com.example.menuka.adaptertest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Person> personList;
private PersonAdapter personAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personList = new ArrayList<>();
personList.add(new Person("Mark", "21"));
personList.add(new Person("John", "22"));
personList.add(new Person("Oliver", "28"));
personList.add(new Person("Tom", "27"));
personList.add(new Person("Francis", "30"));
personAdapter = new PersonAdapter(this, 0, personList);
ListView listView = (ListView) findViewById(R.id.names_list_view);
listView.setAdapter(personAdapter);
}
}
package com.example.menuka.adaptertest;
public class Person {
private String name;
private String age;
public Person(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/text_container">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/name_text_view"
android:gravity="bottom"
android:textSize="18sp"
android:textStyle="normal|bold"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
tools:text="Placeholder Name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/age_text_view"
android:gravity="top"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
tools:text="Placeholder Age"
/>
</LinearLayout>
package com.example.menuka.adaptertest;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class PersonAdapter extends ArrayAdapter<Person>{
private static final String TAG = PersonAdapter.class.getSimpleName();
List<Person> personList;
public PersonAdapter(Context context, int resource, List<Person> objects) {
super(context, resource, objects);
personList = objects;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
Person currentPerson = personList.get(position);
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.person_list_item, parent, false);
}
TextView nameTextView = (TextView) listItemView.findViewById(R.id.name_text_view);
nameTextView.setText(currentPerson.getName());
TextView ageTextView = (TextView) listItemView.findViewById(R.id.age_text_view);
ageTextView.setText(currentPerson.getAge());
return listItemView;
}
}
@pythongiant
Copy link

Aye Man thanks for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment