http://blog.blundell-apps.com/listviewwithfooter/
Moved to my own blog page! Now I can respond to comments faster.
Enjoy:
http://blog.blundell-apps.com/listviewwithfooter/
If you like this post please comment to encourage me to do more.
Blundell






public class ImageListAdapter extends BaseAdapter{
private Context context;
private List<Image> listImage;
public ImageListAdapter(Context context,List<Image> listImage) {
// TODO Auto-generated constructor stub
this.context=context;
this.listImage=listImage;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listImage.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listImage.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Image entry = listImage.get(position);
LayoutInflater inflater = LayoutInflater.from(context);
if(convertView==null)
{
convertView =inflater.inflate(R.layout.row_layout, null);
}
TextView txt_ImageName= (TextView)convertView.findViewById(R.id.txtName);
TextView txt_ImageSize= (TextView)convertView.findViewById(R.id.txtSize);
ImageView txt_ImageView=(ImageView)convertView.findViewById(R.id.ImageView);
// get data in imageName
txt_ImageName.setText(entry.getName());
//get data in ImageSize
txt_ImageSize.setText(entry.getSize());
// convert resource to bitmap
String pathName=entry.getIcon();
try {
File imageFile = new File(pathName);
Bitmap bmp;
bmp=decodeFile(imageFile);
txt_ImageView.setImageBitmap(bmp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//get data in imageview
return convertView;
}
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=64;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
} public class ImageListActivity extends ListActivity {
/** Called when the activity is first created. */
private Cursor myCursor;
List<Image> listImage ;
int itemsPerPage = 15;
boolean loadingMore = false;
ListView listview;
ImageListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview =getListView();
listview.setChoiceMode(2);
listImage = new ArrayList<Image>();
// get all cursor
myCursor=this.managedQuery(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
startManagingCursor(myCursor);
//add footview
View footerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listfooter, null, false);
listview.addFooterView(footerView);
Log.d("0"," thnh cong");
adapter = new ImageListAdapter(this,listImage);
this.setListAdapter(adapter);
// event scroll
listview.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
//what is the bottom iten that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
// listImage = new ArrayList<Image>();
//is the bottom item visible & not loading more already ? Load more !
if((lastInScreen == totalItemCount) && !(loadingMore)){
for (int i=0; i < itemsPerPage; i++) {
myCursor.moveToNext();
//Fill the item with some bogus information
String imageName = myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
String imageSize= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.SIZE));
String imageIcon= myCursor.getString(myCursor.getColumnIndexOrThrow(Images.Media.DATA));
listImage.add(new Image(imageName, imageSize, imageIcon));
Log.d("1","add thnh cong");
}}
adapter.notifyDataSetChanged();
}
});
Return to Code Snippets for Android
Users browsing this forum: KerryDicostanzo and 11 guests