Friday, April 26, 2013

Android, show list of items and bind click on list items

Call list item intent from your application class:
Intent intent = new Intent(MyFirstAppActivity.this, LentList.class);
startActivity(intent);

Here, MyFirstAppActivity is the main class and LentList is the class where list items would show.

First, create a layout file under layout folder, such named list.xml and paste the following content:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>

Now create LentIist.java class:
public class LentList extends ListActivity

private ArrayList<LentPerson> liLentPerson;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    this.generateLentPersons();
    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, liLentPerson.toArray()));
}

Function: generateLentPersons() is as following:
private ArrayList<Person> getPersons() {
    ArrayList<Person> liPerson = new ArrayList<Person>();
    person = new Person(1, "Pritom Kumar", "Software Engineer", "KU");
    return liPerson;
}
Function MyAdapter is as following:
private class MyAdapter extends ArrayAdapter<Object> {
	public MyAdapter(Context context, int resource, Object[] persons) {
		super(context, resource, persons);
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View row = inflater.inflate(R.layout.list_each, parent, false);	
		LentPerson p = (Person) getItem(position);
		
		TextView textView1 = (TextView) row.findViewById(R.id.textView1);
		TextView textView2 = (TextView) row.findViewById(R.id.textView2);	
		TextView textView3 = (TextView) row.findViewById(R.id.textView3);
		
		textView1.setText(p.getName()); /* Pritom Kumar */
		textView2.setText(p.getDesignation()); /* Software Engineer */
		textView3.setText(p.getUniversity()); /* KU */
                /* Bind event on row click */
		row.setOnClickListener(new OnItemClickListener(p));
		return row;
	}
} 
 

Layout file list_each.xml as following:
<?xml version="1.0" encoding="UTF-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </TableLayout>
    
Function OnItemClickListener is as following:
private class OnItemClickListener implements OnClickListener{
	private LentPerson p;
	public OnItemClickListener(LentPerson p) {
		this.p = p;
	}
	public void onClick(View arg0) {
		Toast.makeText(LentList.this, p.getName(), Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(LentList.this, PersonDetails.class);
		intent.putExtra("personId", p.getId());
		intent.putExtra("txtName", p.getName());
		intent.putExtra("txtBorrower", p.getborrower());
		intent.putExtra("txtCategory", p.getCategory());
		startActivity(intent);
	} 
}

Java class  PersonDetails is as following:
package com.pkm.andrd;

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

public class PersonDetails extends Activity {
	private TextView textView2;
	private TextView textView4;
	private TextView textView6;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.persondetails);
		
		textView2 = (TextView) findViewById(R.id.textView2);
		textView4 = (TextView) findViewById(R.id.textView4);
		textView6 = (TextView) findViewById(R.id.textView6);

		textView2.setText(getIntent().getExtras().getString("txtName"));
		textView4.setText(getIntent().getExtras().getString("txtBorrower"));
		textView6.setText(getIntent().getExtras().getString("txtCategory"));
	}
}
Layout  R.layout.persondetails is as following:
<?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/textView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/s2610132158"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >




            <TextView
                android:id="@+id/textView1"
            android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:text="@string/Name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>



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

    </TableRow>



    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >



        <TextView
            android:id="@+id/textView3"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="@string/borrower"
            android:textAppearance="?android:attr/textAppearanceMedium" />



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

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/textView5"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="@string/category"
            android:textAppearance="?android:attr/textAppearanceMedium" />



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

    </LinearLayout>

</LinearLayout>

Make sure you have the following entry in your manifest file under application tag:
<activity android:name=".LentList" />
<activity android:name=".PersonDetails" />

No comments:

Post a Comment