Sophie
Sophie

Reputation: 2634

Count Total Number of List Items in a ListView

How to count Total Number of List Items in a ListView ?

I am writing a Church Application in which i am populating list using images stored into SD Card, but now i want to count total number of list items.

     // to upload whole list
     for(int position = 0; position < lstView.getAdapter().getCount(); position++)
                 {
                     flags.put(position, true);   
                 }

                 ((BaseAdapter) lstView.getAdapter()).notifyDataSetChanged();        
            }
        });

        /*** Get Images from SDCard ***/
        listSDCardImages = fetchSDCardImages();

        // ListView and imageAdapter
        lstView = (ListView) findViewById(R.id.listSDCardImages);
        lstView.setAdapter(new ListSDCardImagesAdapter(this));

        Toast.makeText(getApplicationContext(), "Total number of Items are:" + String.valueOf(position), Toast.LENGTH_LONG).show();
        }

Everytime i am getting 0

Upvotes: 16

Views: 55198

Answers (1)

Amarjit
Amarjit

Reputation: 4357

Total list view count is

 lstView.getAdapter().getCount() ,

So use

Toast.makeText(getApplicationContext(), "Total number of Items are:" + lstView.getAdapter().getCount() , Toast.LENGTH_LONG).show();

Upvotes: 43

Related Questions