Pages

Wednesday, May 23, 2012

Creating listview with custom layout in android

Hi, Here is an example for listview with custom layout.
contentitem.xml

<?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">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearLayout1" android:orientation="vertical">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/linearLayout2" android:padding="10dp">
<LinearLayout android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/linearLayout3" android:orientation="vertical" android:layout_weight="1" android:gravity="center_vertical">
<TextView android:id="@+id/textView1" android:text="TextView" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:paddingLeft="10dp" android:textColor="#1FBCFF"></TextView>
</LinearLayout>
<LinearLayout android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/linearLayout4" android:orientation="vertical" android:layout_weight="1" android:gravity="right">
<TextView android:id="@+id/textView2" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:text="&gt;&gt;&gt;" android:textColor="#666666"></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>

</LinearLayout>

contents.xml

<?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">
<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="match_parent" android:padding="5dp" android:layoutAnimation="@anim/catanimation"></ListView>
</LinearLayout>

TableofContent.java

public class TableofContent extends Activity {
protected ListAdapter adapter;
protected SQLiteDatabase db;
public Cursor cursor;
public ListView lv;
public Context c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contents);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
db = (new DatabaseHelper(this)).getWritableDatabase();
c=this;
lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent = new Intent(c, DetailedView.class);
Cursor cursor2 = (Cursor) adapter.getItem(arg2);
intent.putExtra("topicid", cursor2.getInt(cursor2.getColumnIndex("_id")));
startActivity(intent);
}
});
}
public void onResume() {
super.onResume();
cursor = this.db
.rawQuery("select _id,topic from resumedetails order by topic asc",null);
adapter = new InboxCursorAdapter(this, R.layout.contentitem, cursor, new String[] { "topic" }, new int[] { R.id.textView1 });
lv.setAdapter(adapter);
}
}