[List8.java]
public class List8 extends ListActivity {
PhotoAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_8);
getListView().setEmptyView(findViewById(R.id.empty));
mAdapter = new PhotoAdapter(this);
setListAdapter(mAdapter);
Button clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mAdapter.clearPhotos();
} });
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mAdapter.addPhotos();
} });
}
public class PhotoAdapter extends BaseAdapter {
private Integer[] mPhotoPool = {
R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
private ArrayList<Integer> mPhotos = new ArrayList<Integer>();
public PhotoAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mPhotos.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mPhotos.get(position));
i.setAdjustViewBounds(true);
i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
public void clearPhotos() {
mPhotos.clear();
notifyDataSetChanged();
}
public void addPhotos() {
int whichPhoto = (int)Math.round(Math.random() * (mPhotoPool.length - 1));
int newPhoto = mPhotoPool[whichPhoto];
mPhotos.add(newPhoto);
notifyDataSetChanged();
}
}
}
[list_8.xml]
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_8_new_photo"/>
<Button android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_8_clear_photos"/>
</LinearLayout>
<!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it -->
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/list_8_no_photos"/>
</FrameLayout>
</LinearLayout>
'old > API_Demo' 카테고리의 다른 글
Views_Lists_Single choice list (0) | 2010.04.27 |
---|---|
Views_Lists_Array(Overlay) (0) | 2010.04.27 |
Views_Lists_Cursor(Phones) (0) | 2010.04.27 |
Views_Lists_ListAdapter Collapsed (0) | 2010.04.27 |
Views_Lists_Separators (0) | 2010.04.27 |