views:

302

answers:

1

Hello all, I am making an android application that shows a list with users, I would like to add the image,name,status,and a button in every row of the list, just like in the androids 1.6 native caller-> favorites tab.Until know I have managed to add the image and name using an ListActivity but when I try to add the button the list becomes unselected.So I have 2 questions, first is the list mentioned above listviews or listactivities? also is this possible using listActivity? and second, what is the difference of the above mentioned classes? any link to tutorials would be apreciated.thanks in advanced maxsap.

A: 

You will need to extend ListActivity and ListAdapter to implement your design. The activity that displays your list should extend ListActivity instead of Activity. In the onCreate method of your ListActiviry, the content view of your activity should be set to a linear layout that is the parent of a listview. The list view must have id "@+id/android:list". You can also include a textview to be displayed when the list is empty, see below. Also in OnCreate call setListAdapter() and pass in a new object of your that extends ListAdapter.

In the class that you make that extends ListAdapter, override all of the methods that you need to, especially getView().

Example code:

MyListActivity.java

import android.app.ListActivity;
public class MyListActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_list);
        setListAdapter(new MyListAdapter());
    }
}

my_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android:="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <ListView android:id="@+id/android:list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
    <TextView android:id="@+id/android:empty"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="No Events!"/>
</LinearLayout>

MyListAdapter.java

import android.widget.ListAdapter;

public class MyListAdapter implements ListAdapter {
  //Methods to load your data

  public View getView(int arg0, View reuse, ViewGroup parent) {
    //Create the view or if reuse is not null then reuse it.
    //Add whatever kind of widgets you want here and return the view object
  }
}
Segfault
You do not need `ListActivity` to use a `ListView`. Also, I advise against using activities as the contents of tabs -- just put in views for greater efficiency and less confusion. Otherwise, though, you're on target.
CommonsWare
good advice CommonsWare, I made it like that because he was specifically asking about ListActivity.
Segfault