2014-05-17 40 views
12

如何计算ListView中的列表项总数?计数ListView中的列表项总数

我正在写一个教会应用程序,其中我使用存储在SD卡中的图像填充列表,但现在我要计算列表项的总数。

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

每次我得到

回答

30

总列表视图计数

lstView.getAdapter().getCount() , 

所以使用

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

感谢解决我做了愚蠢的错误,我可以知道如何将这个值赋给String变量(在TextView中显示),我会接受你的答案a按照SO规则,需要5分钟 – Sophie

+0

是的,为什么不是String count =“”+ lstView.getAdapter()。getCount(); textView.setText(计数); – theLazyFinder

+0

在ListView中,我在使用红色圆圈和许多列表项目使用蓝色的许多列表项目中使用了两个不同的图像,所以如果我想要计数列表项目包含红色圆圈并列出那些包含蓝色圆圈的项目,请检查以下内容: http://pastebin.com/ENWcQSmM – Sophie