This source code shows a simple example of how to create an Android ListActivity and ListView, using a static array of elements for the list.
package com.devdaily.android.twitterclient;
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 TwitterClient extends ListActivity
{
String[] values = { "Stream", "My Lists", "My Searches", "Trends" };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
// parameters are (Context, layout for the row, and the array of data)
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Post new comment