ListView (Displaying String Array)

ListView (Displaying String Array)

/res/layout/list_item.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">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/txt" />
	<ListView android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:id="@+id/List" />
</LinearLayout>

Activity file

package com.androidwork.adapterdemo;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Gallery;
import android.widget.ListView;
import android.widget.TextView;

public class AdapterDemoActivity extends Activity {
	/** Called when the activity is first created. */
	ListView list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_item);
		final String[] items = new String[] { "Item1", "Item2", "Item3",
				"Item4" };

		ArrayAdapter ad = new ArrayAdapter(
				getApplicationContext(), android.R.layout.simple_list_item_1,
				items);
		
		list = (ListView) findViewById(R.id.List);
		list.setAdapter(ad);
		// list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				TextView txt = (TextView) findViewById(R.id.txt);
				txt.setText("It is : " + arg2);
			}
		});

	}
}

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s