2014-05-17 38 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