Tuesday, January 29, 2013

Android Custom List View

We can make a custom list view in an interactive manner which is easy to understand and easy to play around. for this purpose make a project with the name CustomListView and have a main activity as

1) CustomListViewExample.java


package com.jitesh.customlistview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomListViewExample extends Activity {
List<Student> model = new ArrayList<Student>();
StudentAdapter adapter = null;

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

Button save = (Button) findViewById(R.id.save);

save.setOnClickListener(onSave);

ListView list = (ListView) findViewById(R.id.students);
adapter = new StudentAdapter();
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

Toast.makeText(CustomListViewExample.this,
position + "Clicked!", Toast.LENGTH_LONG).show();
AlertDialog.Builder adb = new AlertDialog.Builder(
CustomListViewExample.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
model.remove(positionToRemove);

adapter.notifyDataSetChanged();
}
});
adb.show();
}

});

}

private View.OnClickListener onSave = new View.OnClickListener() {
public void onClick(View v) {
Student r = new Student();
EditText name = (EditText) findViewById(R.id.name);
EditText address = (EditText) findViewById(R.id.addr);

r.setName(name.getText().toString());
r.setAddress(address.getText().toString());

adapter.add(r);
}
};

class StudentAdapter extends ArrayAdapter<Student> {
StudentAdapter() {
super(CustomListViewExample.this, R.layout.row, model);
}

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
StudentHolder holder = null;

if (row == null) {
LayoutInflater inflater = getLayoutInflater();

row = inflater.inflate(R.layout.row, parent, false);
holder = new StudentHolder(row);
row.setTag(holder);
} else {
holder = (StudentHolder) row.getTag();
}

holder.populateFrom(model.get(position), position);

return (row);
}
}

static class StudentHolder {
private TextView name = null;
private TextView address = null;
private ImageView icon = null;

StudentHolder(View row) {
name = (TextView) row.findViewById(R.id.title);
address = (TextView) row.findViewById(R.id.address);
icon = (ImageView) row.findViewById(R.id.icon);
}

void populateFrom(Student r, int position) {

name.setText(r.getName());
address.setText(r.getAddress());
boolean even = false;
boolean prime = true;

if (position % 2 == 0) {
even = true;
prime = false;
} else {
for (int i = 3; i * i <= position; i += 2) {
if (position % i == 0) {
prime = false;
}
}
}

if (even) {
icon.setImageResource(R.drawable.ball_red);
} else {
if (prime) {
icon.setImageResource(R.drawable.ball_green);
} else {
icon.setImageResource(R.drawable.ball_yellow);
}
}

}
}
}

2) Student.java


package com.jitesh.customlistview;

public class Student {

 private String studentname="";
 private String collegeaddress="";
 private String standard="";

 public String getName() {
   return(studentname);
 }

 public void setName(String name) {
   this.studentname=name;
 }

 public String getAddress() {
   return(collegeaddress);
 }

 public void setAddress(String address) {
   this.collegeaddress=address;
 }

 public String getType() {
   return(standard);
 }

 public void setType(String stnd) {
   this.standard=stnd;
 }

 public String toString() {
   return(getName());
 }
}

3) main.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="match_parent" >

    <TableLayout
        android:id="@+id/details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:shrinkColumns="1"
        android:stretchColumns="1" >

        <TableRow>

            <TextView android:text="Name:" />

            <EditText android:id="@+id/name" />
        </TableRow>

        <TableRow>

            <TextView android:text="Address:" />

            <EditText android:id="@+id/addr" />
        </TableRow>

        <Button
            android:id="@+id/save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Save" />
    </TableLayout>

    <ListView
        android:id="@+id/students"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/details"
        android:layout_alignParentTop="true" />

</RelativeLayout>

4) row.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:padding="4dip"
  >
  <ImageView android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="4dip"
  />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView android:id="@+id/title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center_vertical"
      android:textStyle="bold"
      android:maxLines="1"
      android:ellipsize="end"
    />
    <TextView android:id="@+id/address"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center_vertical"
      android:maxLines="1"
      android:ellipsize="end"
    />
  </LinearLayout>
</LinearLayout>





You can download the code as well from here  CustomListView



No comments:

Post a Comment