In this example, we show you how to display a list of fruit name via
ListView
, it should be easy and self-explanatory.
1.1 Android Layout file
File : res/layout/list_fruit.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="20sp" > </TextView>
1.2 ListView
package com.Abhi.android; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ListFruitActivity extends ListActivity { static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana", "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit", "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // no more this // setContentView(R.layout.list_fruit); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS)); ListView listView = getListView(); listView.setTextFilterEnabled(true); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } }
1.3 Screen shot
Try this and check the same in your android phone.
No comments:
Post a Comment