Sophie
Sophie

Reputation: 2634

Count Total Number of Items based on Condition

In my program i am using ListView and in each and every row i am showing one of the image icon (Yes/No) based on condition i am playing with, and using below code to get total number of item(s) in a list (includes both :- Yes & No) Icons.

String totalNumberOfItemsInAList = ""+ lstView.getAdapter().getCount();

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

But what if i only want to know total number of items in a list which contains Yes Icon, my code looks like this:

     private SparseBooleanArray flags = new SparseBooleanArray();

     // 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();
        }

Condition, i am using to show Yes/No icons is something like this:

if(resultAvailable)
{
holder.colView.setImageResource(R.drawable.icon_yes);
}
else
{
holder.colView.setImageResource(R.drawable.icon_no);
}

Upvotes: 0

Views: 345

Answers (1)

Giru Bhai
Giru Bhai

Reputation: 14398

Use

yesImagesCount=0;

variable in your activity and increment this count in this code as

if(resultAvailable)
{
    holder.colView.setImageResource(R.drawable.icon_yes);
    yesImagesCount++;
}
else
{
    holder.colView.setImageResource(R.drawable.icon_no);
}

And Finally display toast as

Toast.makeText(getApplicationContext(), "Total number of Yes  Icons are:" + String.valueOf(yesImagesCount), Toast.LENGTH_LONG).show(); 

Upvotes: 1

Related Questions