List View example in android/ program to create listview,simple listview/Listview using list activity/listview tutorials
The display of elements in a list is a very common pattern in mobile applications. The user gets a list of items and can scroll through them. If he selects one item this usually triggers something else.
ListView
is one of Android's most widely used widgets. It is capable of displaying a scrollable list of items. These items can be of any type. The data to the ListView will be provided by ListAdapterTo add ListView to any activity, we have to include <
ListView
> tag to the activity’s layout xml. The Activity class that uses ListView should implement the ListActivity instead of plain Activity class. ListView using ListActivity
1. Create your activity class by extending ListActivity instead of extending Activity
public classHelloListactivityActivity extends ListActivity {
2. Create a String array that will be placed in the ListView
String androidphones[]={"Sony Ericson Xperia","Motorola Droid", "Samsung Galaxy","HTC Sensation","LG Nitro","Google Nexus"};
3. Use Array Adapter to list the data
setListAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,androidphones));
4. Item click on ListView
public voidonListItemClick(ListView lv,View v,int position, long id)
{
super.onListItemClick(lv, v, position, id);
5. Get name of clicked item
String item=getListView().getItemAtPosition(position).toString();
Full Source code
Result
ListView with images
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="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_height="70dip" android:id="@+id/linearLayout1" android:layout_width="match_parent"> <TextView android:text="TextView" android:layout_width="200dip" android:layout_height="wrap_content" android:id="@+id/textView1"> </TextView> <ImageView android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_height="wrap_content" android:layout_width="wrap_content"> </ImageView> </LinearLayout> </LinearLayout> |
HelloListactivityActivity.java
package hello.list.activity; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class HelloListactivityActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String androidphones[]={"Sony Ericson Xperia","Motorola Droid", "Samsung Galaxy","HTC Sensation","LG Nitro","Google Nexus"}; setListAdapter(new ArrayAdapter<String>(this, R.layout.main,R.id.textView1,androidphones)); } public void onListItemClick(ListView lv,View v,int position, long id) { super.onListItemClick(lv, v, position, id); String item=getListView().getItemAtPosition(position).toString(); Toast.makeText(this, item, Toast.LENGTH_LONG).show(); } } |
not working.. provide full information
ReplyDeleteon imageView . it gives error "content discription not found"
as a beginner to android , i cant understand example
what is linearLayout?? i dnt knw