RSS

Search Engine

Saturday, November 13, 2010

Simple Listview Example In Android

Hello Friends,

Today we have discussed about the liastview. If you are new for android developement. You must know about the develop listview.

And this are the basic tutorials for android application. And you make lots of think for android application.

This listview are simple and this listview code given below.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true"
>
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="2px" android:layout_marginLeft="10px" android:layout_marginRight="10px"
android:layout_marginTop="70px" android:layout_marginBottom="10px"
android:paddingLeft="15px" android:paddingRight="20px" />
</LinearLayout>

And here is java code.

listview.java

package com.android;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class Listview extends Activity {

private ListView list1;

private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

"item 1", "item 2", "item3", "item 4" };

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

list1 = (ListView) findViewById(R.id.ListView01);

// By using setAdpater method in listview we an add string array in

// list.

list1.setAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_1, array));

}

}

Download Source Code

And output given below.



4 comments:

Anonymous said...

This code is showing me error in these 2 places
R.id.ListView01
android.R.layout.simple_list_item_1

Murali said...

i wrote code for onListItemClick to above listview code. it shows Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
error in logcat.

my code for OnListItemClick is
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}

please help me.

Anonymous said...

thanks a lot!! its a very simple and nice example!!

Demasia said...

Good Example. Thanks!

www.themartianview.com

Post a Comment