[TUT] ListView with a Footer Item

Quickly share your Android Code Snippets here...

[TUT] ListView with a Footer Item

Postby blundell » Sun Nov 28, 2010 2:57 pm

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
Attachments
lv.png
ListView with footer EditText
lv.png (120.31 KiB) Viewed 5153 times
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: [TUT] ListView with a Footer Item

Postby skydeaner » Thu Jan 20, 2011 7:36 am

dude, thanks bunches and please do more mini tuts like this
skydeaner
Freshman
Freshman
 
Posts: 7
Joined: Tue Jan 18, 2011 10:01 pm

Re: [TUT] ListView with a Footer Item

Postby Luki » Tue Mar 01, 2011 9:19 am

Crazy Guy you are. I searched for this solution for a very long time. Thanks mate!
Luki
Developer
Developer
 
Posts: 32
Joined: Fri Jul 24, 2009 12:59 pm

Re: [TUT] ListView with a Footer Item

Postby blundell » Mon Oct 24, 2011 2:24 pm

No problem, enjoy!
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: [TUT] ListView with a Footer Item

Postby leethaoqn » Fri Nov 18, 2011 6:28 am

Hi, guy
I want load 10 item in list . when i scroll end list , it will load 10 item next . How do i do it?
leethaoqn
Freshman
Freshman
 
Posts: 7
Joined: Wed Sep 21, 2011 3:34 pm

Re: [TUT] ListView with a Footer Item

Postby blundell » Mon Nov 21, 2011 9:28 am

WIthin your adapter, in the getView() method, check to see if your at the bottom of the list (if i==10). If you are, clear your list and load your next 10 items.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: [TUT] ListView with a Footer Item

Postby leethaoqn » Mon Nov 21, 2011 4:29 pm

here is my code , may you help me fix it ?
Class adapter :
Code: Select all
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;
    }

Class activity
Code: Select all
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();
         }
         
      });
leethaoqn
Freshman
Freshman
 
Posts: 7
Joined: Wed Sep 21, 2011 3:34 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 4 guests