Skip to content

Instantly share code, notes, and snippets.

@manuelzs
Created March 29, 2011 17:28
Show Gist options
  • Save manuelzs/892817 to your computer and use it in GitHub Desktop.
Save manuelzs/892817 to your computer and use it in GitHub Desktop.
Layout file for creating a simple ListView in an activity
package com.example.listview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Item> {
private Context _context;
private ArrayList<Item> _items;
private int _viewResourceId;
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<Item> items) {
super(context, textViewResourceId, items);
this._items = items;
this._context = context;
this._viewResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(_viewResourceId, null);
}
Item i = _items.get(position);
if (i != null) {
ImageView image1 = (ImageView) v.findViewById(R.id.image1);
ImageView image2 = (ImageView) v.findViewById(R.id.image2);
TextView itemtext = (TextView) v.findViewById(R.id.itemtext);
if (image1 != null) {
image1.setImageResource(R.drawable.icon);
}
if (image2 != null) {
image2.setImageResource(R.drawable.icon);
}
if (itemtext != null) {
itemtext.setText(i.get_name());
}
}
return v;
}
}
package com.example.listview;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
public class CustomListView extends ListActivity {
private CustomAdapter _adapter;
private ArrayList<Item> _items;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_items = new ArrayList<Item>();
// Fill the ArrayList with some items. You should define your own
// methods to fill the ArrayList
addItems();
// Set the custom adapter as the adapter for the list view
this._adapter = new CustomAdapter(this, R.layout.row, _items);
setListAdapter(this._adapter);
}
private void addItems() {
for (int i = 0; i < 10; i++) {
_items.add(new Item(i, "item " + i));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView android:id="@+id/image1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="6dip"/>
<ImageView android:id="@+id/image2" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="6dip"/>
<TextView android:id="@+id/itemtext" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment