My Blog List

Wednesday 22 February 2012

How to add text to the ListView in Android


Hello frineds
This post will help you  to  add items to the Listview in android

Here is the output


Android comes with ListView Widget
There is two way to add item(String) to the list view
1st:
 Add String Array resource  in string.xml
Here is my string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, ListviewExampleActivity!</string>
    <string name="app_name">ListviewExample</string>
    <string-array name="ListItem">
        <item >Red</item>
        <item >Green</item>
        <item >Blue</item>
        <item >Yellow</item>
        <item >Cyan</item>
       
    </string-array>

</resources>



Now add  android:entries=”@array/ListItem” to the ListView Widget
Here is my main.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

  

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/ListItem">
    </ListView>

</LinearLayout>


2nd way

Create ArrayAdapter  having String Array resource and set it to the Listview
Here is my ListView.java Activity

package com.bp.listview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListviewExampleActivity extends Activity {
ListView l;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       String  arr[]={"Red","Green","Blue","Yellow","Cyan"};
      l=(ListView) findViewById(R.id.listView1);
  ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arr);
  l.setAdapter(adapter);
  
        
    }
}

enjoy coding
for any query feel free to write @ bpsingh216@gmail.com
comment if it is usefull..


13 comments:

  1. Thank you.
    I want to add something more in this. Say this list already exists and now I've taken EditText and i want to enter another Color in this list after pressing ADD COLOR button.
    Can you please help me withh the code?

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Android training in bangalore

    ReplyDelete
  3. Best institute for 3d Animation and Multimedia Course training Classes


    Best institute for 3d Animation and Multimedia

    Best institute for 3d Animation Course training Classes in Noida- webtrackker Is providing the 3d Animation and Multimedia training in noida with 100% placement supports. for more call - 8802820025.

    3D Animation Training in Noida

    Company Address:

    Webtrackker Technology

    C- 67, Sector- 63, Noida

    Phone: 01204330760, 8802820025

    Email: info@webtrackker.com

    Website: http://webtrackker.com/Best-institute-3dAnimation-Multimedia-Course-training-Classes-in-Noida.php

    ReplyDelete
  4. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete

  5. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  6. Thanks For Sharing The Information The information Shared Is Very valuable Please keep updating us Time Just Went On reading The article Python Online Course AWS Online Course Devops Online Course DataScience Online Course

    ReplyDelete
  7. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Data science Related information please visit our website Data science courses in Pune page!

    ReplyDelete