Monday, August 30, 2010

Android: ListActivity, ListView and ListAdapter

How to display list view?

1. use ArrayAdapter

(1) create ListActivity
(2) set data source to ArrayAdapter
(3) get ListView from ListActivity
(4) set adapter to ListView
(5) set click listener for each list item

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDataSource = getResources().getStringArray(R.array.countries_array);
mListAdapter = new ArrayAdapter(this, R.layout.text_item, mDataSource);
setListAdapter(mListAdapter);

ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast t = Toast.makeText(TextListViewActivity.this, mDataSource[position], Toast.LENGTH_SHORT);
t.show();
}
});
}


2. customize adapter

(1) create ListActivity
(2) create CustomizedAdapter which extends BaseAdapter
(3) implement getCount/getItem/getItemId according to your data source
(4) create layout from list item
(5) implement getView, use LayoutInflater to set view of item

public class CustomizedAdapter extends BaseAdapter {
protected String[] mDataSource = getResources().getStringArray(R.array.countries_array);

@Override
public int getCount() {
return mDataSource.length;
}

@Override
public Object getItem(int position) {
return mDataSource[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.compound_image_text_view_horizontal, null);
TextView tv = (TextView) v.findViewById(R.id.itemText);
tv.setText(mDataSource[position]);
return v;
}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearItem"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="5sp" android:layout_marginBottom="5sp"
android:src="@drawable/icon"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemText"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10sp" android:layout_marginBottom="10sp"
android:textSize="24sp"/>
</LinearLayout>

(6) set adapter to ListView
(7) set click listener for each list item

3. customize adapter and change theme of ListView

set view to ListActivity

<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="true"
android:background="@color/fade_white"
android:cacheColorHint="@color/fade_white"
android:divider="@drawable/gray_divider"
android:dividerHeight="1dp"
android:focusable="false"
android:scrollingCache="false"/>


ref:
1. Hello, ListView
2. ListAdapter

No comments:

Post a Comment