开发者

Making a list view like the favorite tab on native dialer in android

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开发者_StackOverflow社区 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 appreciated.


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
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜