Custom Adapter List View

Step 1: Create Custom Data Class

public class Person {
	public String firstName;
	public String surname;

	public Person(String firstName, String surname) {
		this.firstName = firstName;
		this.surname = surname;
	}

	@Override
	public String toString() {
		return surname + ", " + firstName;
	}
}

Step 2: Create View to Display

<?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="horizontal" >


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

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        <TextView
            android:id="@+id/txtSurname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

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

</LinearLayout>

Step 3: Create Custom Adapter Class

public class PersonAdapter extends ArrayAdapter<Person> {
	public PersonAdapter(Context context, List<Person> people) {
		super(context, R.layout.person_layout, people);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// get the inflater that will convert the person_layout.xml file into an
		// actual object (i.e. inflate it)
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		// create a view to display the person's info - NOTE different than when
        // inflating an Activity's view
		View personView = inflater.inflate(R.layout.person_layout, parent, false);
		
		// keep track of person this view is working with
		personView.setTag(getItem(position));
		
		// get text views that will hold strings
		TextView txtSurname = (TextView) personView.findViewById(R.id.txtSurname);
		TextView txtFirstname = (TextView) personView.findViewById(R.id.txtFirstname);
		
		// set text fields
		txtSurname.setText(getItem(position).surname);
		txtFirstname.setText(getItem(position).firstName);
		
		// return view to ListView to display
		return personView;
	}
}

Step 4: Link to List View

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get views
        Button btnAdd = (Button) findViewById(R.id.btnAdd);
        ListView lstInfo = (ListView) findViewById(R.id.lstInfo);

        // create data (model)
        final List<Person> items = new ArrayList<Person>();
        items.add(new Person("John", "Doe"));
        items.add(new Person("Jane", "Soap"));
        items.add(new Person("Solly", "Someone"));

        // create adapter to transform string items
        final PersonAdapter adapter = new PersonAdapter(this, items);

        // attach adapter to list view
        lstInfo.setAdapter(adapter);

        // add more items when clicked
        btnAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                adapter.add(new Person("New", "Person"));
            }
        });

        lstInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(Example07.this,
                        "Clicked item " + position, Toast.LENGTH_LONG).show();
            }
        });
    }

Contact Layout

Contact

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

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:padding="10dp">
        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/txtFName"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Medium Text"
                    android:id="@+id/txtLName" android:layout_alignParentTop="true"
                    android:layout_toRightOf="@+id/txtFName" android:layout_toEndOf="@+id/txtFName"
                    android:layout_marginLeft="10dp"/>
            <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true" android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true">
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="Tel."
                        android:id="@+id/lblTel"
                        android:layout_marginLeft="58dp" android:layout_marginStart="58dp"
                        android:layout_marginTop="30dp"
                        android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"/>
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="Medium Text"
                        android:id="@+id/txtTel"
                        android:layout_alignTop="@+id/lblTel" android:layout_toRightOf="@+id/lblTel"
                        android:layout_toEndOf="@+id/lblTel" android:layout_marginLeft="54dp"
                        android:layout_marginStart="54dp"/>
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="Email"
                        android:id="@+id/lblEmail"
                        android:layout_below="@+id/lblTel" android:layout_alignLeft="@+id/lblTel"
                        android:layout_alignStart="@+id/lblTel" android:layout_marginTop="15dp"/>
                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="Medium Text"
                        android:id="@+id/txtEmail" android:layout_alignTop="@+id/lblEmail"
                        android:layout_alignLeft="@+id/txtTel" android:layout_alignStart="@+id/txtTel"/>
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

Contact Edit

<?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:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context="nmu.wrap301.task1.ContactEditActivity"
              android:weightSum="1"
>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/view_con"
            android:layout_marginTop="10dp" android:id="@+id/txtHeading"
            android:layout_gravity="center_horizontal"/>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="70dp" android:layout_marginTop="30dp">
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="name"
                android:id="@+id/txtFName" android:layout_marginTop="10dp" android:layout_marginLeft="20dp"
                android:inputType=""/>
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="surname"
                android:id="@+id/txtLName" android:layout_marginLeft="20dp" android:layout_marginTop="10dp"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="81dp">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Cell:"
                android:layout_marginLeft="20dp" android:layout_marginTop="20dp"/>
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="cell"
                android:id="@+id/txtCell" android:layout_marginLeft="20dp" android:layout_marginTop="20dp"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="73dp">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Email:"
                android:layout_marginLeft="20dp" android:layout_marginTop="20dp"/>
        <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="email"
                android:id="@+id/txtEmail" android:layout_marginTop="20dp" android:layout_marginLeft="20dp"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Edit"
                android:id="@+id/btnEdit" android:layout_marginLeft="20dp" android:onClick="startEdit"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delete"
                android:id="@+id/btnDelete" android:onClick="deleteContact"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Save"
                android:id="@+id/btnSave" android:onClick="saveContact"/>
    </LinearLayout>
</LinearLayout>

Contact List

<?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:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context="nmu.wrap301.task1.ContactListActivity"
              android:weightSum="1">

    <ListView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:id="@+id/lstContacts" android:layout_weight="0.97"/>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:weightSum="1">
        <RadioGroup
                android:layout_width="209dp"
                android:layout_height="match_parent"
                android:orientation='horizontal'
                android:id="@+id/radSort" android:layout_weight="0.73">
            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Tel"
                    android:id="@+id/radTel" android:checked="false"/>
            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Surname"
                    android:id="@+id/radLName" android:checked="false"/>
            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Email"
                    android:id="@+id/radEmail" android:checked="false"/>
        </RadioGroup>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sort"
                android:id="@+id/btnSort"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+"
                android:id="@+id/btnAdd" android:nestedScrollingEnabled="false" android:onClick="addContact"/>
    </LinearLayout>
</LinearLayout>

Test 2 Reflection

Reflective Property

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by ydvma4-15 on 2017/05/12.
 */
public class ReflectiveProperty<T> {

    private Object obj;
    private Method getter = null;
    private Method setter = null;

    private Method getGetter(Object object, String propertyName, Class propertyType){
        Class clazz = object.getClass();
        propertyName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        Method method;
        try {
            method = clazz.getDeclaredMethod("get" + propertyName,new Class<?>[0]);
            //check that the return types are equal
            if(method.getReturnType() != propertyType){
                method = null;
            }
        } catch (NoSuchMethodException e) {
            method = null;
        }
        return method;
    }

    private Method getSetter(Object object, String propertyName, Class propertyType){
        Class clazz = object.getClass();
        propertyName = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        Method method;
        try {
            method = clazz.getDeclaredMethod("set" + propertyName, propertyType);
        } catch (NoSuchMethodException e) {
            method = null;
        }
        return method;
    }

    public ReflectiveProperty(Object object, String propertyName, Class propertyType){
        obj = object;
        setter = getSetter(obj, propertyName, propertyType);
        getter = getGetter(obj, propertyName, propertyType);
    }

    public void set(T value){
        if(setter != null){
            try {
                setter.invoke(obj, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    public T get(){
        if(getter != null){
            try {
                return (T)getter.invoke(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean isReadable(){
        return getter != null;
    }

    public boolean isWriteable(){
        return setter != null;
    }
}

Reflective Test

public class ReflectiveTest {

    public static void main(String[] args) {
        Tester tester = new Tester();

        System.out.println("Creating the reflective property objects");
        ReflectiveProperty<String> testerName = new ReflectiveProperty<>(tester, "name", String.class);
        ReflectiveProperty<Integer> testerAge = new ReflectiveProperty<>(tester, "age", Integer.class);
        System.out.println();

        // display whether readable and/or writeable
        System.out.println("Reflective property objects' readability and writeability");
        System.out.println("Name is readable = " + testerName.isReadable());
        System.out.println("Name is writeable = " + testerName.isWriteable());
        System.out.println("Age is readable = " + testerAge.isReadable());
        System.out.println("Age is writeable = " + testerAge.isWriteable());
        System.out.println();

        // set the property values using the reflective property
        System.out.println("Set property values using reflective property objects");
        testerName.set("Bob");
        testerAge.set(30);
        System.out.println();

        // get the property values using the reflective property
        System.out.println("Displaying property values using the reflective property objects");
        System.out.println("Tester's name = " + testerName.get());
        System.out.println("Tester's age = " + testerAge.get());
        System.out.println();

        // get the values using the methods provided by the tester class
        System.out.println("Displaying the property values using the tester class methods");
        System.out.println("Tester's getName = " + tester.getName());
        System.out.println("Tester's getAge = " + tester.getAge());
    }
}

Tester

public class Tester {
    // field storing the 'name' property's value
    private String name;

    // property called 'name' that is reable and writeable
    public void setName(String name) { this.name = name; }
    public String getName() { return name; }

    // property called 'age' that is only readable
    public Integer getAge() { return 20; }
}

GitHub – LadySith

Yaa

Lusaka, Zambia

Computer Science student and a bunch of other things.