Friday, 22 June 2012

Array adapter list view in android

Array adapter list view in android


ArrayAdapterDemo.java




package a.b;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import  android.widget.ListView;
import android.widget.TextView;
public class  ArrayAdapterDemo extends ListActivity{
TextView t1;
String[] items= { "jithu", "is", "*", "really",
"good", "really3","really4",
"welcome", "jithu", "hai"};
@Override
public  void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));
t1=(TextView)findViewById(R.id.t1);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String text = " position:"+ position + " "+ items[position];
t1.setText(text);
}
}

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"
    android:background="#ffff0000"
    >
<TextView
android:id="@+id/t1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <ListView
android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false">
    </ListView>
    <TextView
    android:id="@+id/empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="empty list"/>
   
</LinearLayout>


output







2 comments:

  1. Nice. I will learn that in a while. But please, put some explanatory comments of what you are doing or how that works. For example, I don't know if that a a tutorial, or a code that you don't know why doesn't work. Since this is your blog, it's suppose that it is a tutorial, but you see? It doesn't say even that anywhere. It's just my advise. I'd hope be well receive. Thanks for your effort yo help us that are starting.

    ReplyDelete
    Replies
    1. it will be surely consider..from the next post itself it have some explanation about the task..thanks for your valuable comment.

      Delete