How to get the number of list items from a list?

I use the code below to track my list, it works fine if the elements of the list are visible.

If the list scrolls, then invisible elements do not gain access to this code. How to navigate through all elements of a list that are visible + invisible elements.

for(int i=0;i<list.getCount;i++) { View v = list.getChildAt(i); TextView tv= (TextView) v.findViewById(R.id.item); Log.d("","ListItem :"+tv.getText()); } 
+7
source share
2 answers

I recently did this. Suppose I want an invisible button in a listItem element. Then, in the getView of the list adapter, add this button to the global vector. as shown below.

  Button del_btn = viewCache.getFrame(); view_vec.add(del_btn); 

Here, viewCache is an object of the ViewCache class, which looks like this:

  class ViewCache { private View baseView; private Button button; public ViewCache(View baseView) { this.baseView = baseView; } public Button getButton() { if(button == null) { button = (Button) baseView.findViewById(R.id.DeleteChatFrndBtn); } return button; } } //it is necessary sometimes because otherwise in some cases the list scroll is slow. 

Now you like to see the listItem button when you click on another button. Then the code looks below -

  public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.EditChatFrndBtn: length = view_vec.size(); for(int i = 0; i < length; i++) { Button btn = (Button) view_vec.elementAt(i); btn.setVisibility(View.VISIBLE); } doneBtn.setVisibility(View.VISIBLE); editBtn.setVisibility(View.INVISIBLE); break; } } 

Instead of R.id.EditChatFrndBtn put your button identifier by clicking on which you will be an invisible / visible listItem button.

+3
source

Here is how you can get your ListView in Activity and go through it.

 ListView myList = getListView(); int count = myList.getCount(); for (int i = 0; i < count; i++) { ViewGroup row = (ViewGroup) myList.getChildAt(i); TextView tvTest = (TextView) row.findviewbyid(R.id.tvTestID); // Get your controls from this ViewGroup and perform your task on them =) } 

I hope this helps

+4
source

All Articles