package com.google.android.samples.view;
import com.google.android.samples.R;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* A list view that demonstrates the use of setEmptyView. This example alos uses
* a custom layout file that adds some extra buttons to the screen.
*/
public class List8 extends ListActivity {
PhotoAdapter mAdapter;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Use a custom layout file
setContentView(R.layout.list_8);
// Tell the list view which view to display when the list is empty
getListView().setEmptyView(findViewById(R.id.empty));
// getListView().setHorizontalScrollBarEnabled(true);
// getListView().setVerticalScrollBarEnabled(true);
// Set up our adapter
mAdapter = new PhotoAdapter(this);
setListAdapter(mAdapter);
// Wire up the clear button to remove all photos
Button clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mAdapter.clearPhotos();
} });
// Wire up the add button to add a new photo
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mAdapter.addPhotos();
} });
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class PhotoAdapter extends BaseAdapter {
int photoIndex = 0;
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<PhotoView> mPhotos = new ArrayList<PhotoView>();
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) {
return mPhotos.get(position);
}
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(new PhotoView(mContext,newPhoto, "Hey, Looking good at " + whichPhoto, photoIndex++));
// mPhotos.add(R.layout.block);
notifyChange();
}
private class PhotoView extends LinearLayout {
int offset = 1;
public PhotoView(Context context, int position, String title, int offset_) {
super(context);
offset = offset_;
this.setOrientation(VERTICAL);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
// Here we build the child views in code. They could also have
// been specified in an XML file.
// LinearLayout layout = findViewById(R.id.b);
TextView titleText = (EditText) findViewById(R.id.block_id_field);
titleText.setText("3.989");
int leftMarginOffset = (offset*10);
p.setMargins(leftMarginOffset, 0, 0, 0);
addView(titleText, p);
}
}
}
}