Skip to main content

ListView using ListActivity

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 ListAdapter
To 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();
    }
}
Result 






Comments

  1. not working.. provide full information

    on imageView . it gives error "content discription not found"

    as a beginner to android , i cant understand example

    what is linearLayout?? i dnt knw

    ReplyDelete

Post a Comment

Popular posts from this blog

Capture image without surface view as background service in android

Capture image without user knowledge in android/Capture image as background service in android/Take photo without preview in android/ Camera without preview in android  package com.example.picture; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.AudioManager; import android.os.IBinder; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraService extends Service {       //Camera variables       //a surface holder       private SurfaceHolder sHolder ;        //a variable to control the camera       private Camera mCamera ;       //the camera parameters       private Parameters parameters ;       /** Called when the activity is first created. */     @Override     p

Custom Spinner in Android

Custom Spinner in Android/Spinner using custom adapter/spinner with icon and text  In this tutorial, i'll show you to customizing spinner in different methods.  To change the normal display of spinner view, add background image using the code " android:background="@drawable/myspinner_background" in the xml. You can also make it customizable by setting up an xml file in drawable folder Custom display with icon in the spinner   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"     >     < Spinner     android:drawSelectorOnTop = "true"     android:id = "@+id/spinner1"     android:layout_width = "match_parent"     android:layout_height = "wrap_content&

Coroutine- A complete tutorial

 What are coroutines? To know about coroutine, first you need to know about asynchronous programming, thread and multithreading concept. What is a thread? Thread describes in which context the function or sequence of instructions should be executed. So, every block of code or functions runs in a thread, right? Also, you can load multiple threads to perform different block of codes  How to start a thread? Thread thread = new Thread(){     public void run(){       System.out.println("This is a thread");     }   }     thread.start(); So when coming to android, before learning about coroutines we need to discuss some scenarios. Normally how an app works when user launch an application. When user launchers the application, a main thread is created. This thread is intended to do small operations like button clicks, UI interaction , small mathematical operations. We cant perform long running operation like file download, database queries, network operations and image