Malvinka
Malvinka

Reputation: 1379

listview - access to non-visible items

I would like to check which of elements on my listview is also a member of another list and check all of them (by changing background). But the only way I can think of is:

for (String str : list1){
    if (list2.contains(str)) {
       lv.getChildAt(adapter.getPosition(str)).setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
    }
}

But that works only for visible elements of the list and throws null pointer exception when accessing non-visible elements. What can I do to apply changes for all list items? Do I have to write my own adapter or maybe there is any "equivalent" of getChiledAt but working for all elements of the listview not only visible ones?

Upvotes: 0

Views: 240

Answers (3)

Malvinka
Malvinka

Reputation: 1379

The solution was to use onScroll method (as Chitrang suggested) and set everything only for visible item. Android Magic works, everything works fine, also for non-visible items :)

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      for (int i = lv.getFirstVisiblePosition(); i<=lv.getLastVisiblePosition(); i++){
            if (list2.contains(lv.getItemAtPosition(i).toString()))
                lv.getChildAt(i - lv.getFirstVisiblePosition()).setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
            }
       }
}

Upvotes: 1

Chitrang
Chitrang

Reputation: 5097

I didn't try by myself, but a suggestion. Can you please try this way, and check.

Idea is to use setOnScrollListener with onScroll method and to have null check inside for loop. It's not good solution though, because for loop working on every scroll.

        lv.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            for (String str : list1) {

                if (list2.contains(str)) {
                    if (lv.getChildAt(adapter.getPosition(str)) != null) {

                        lv.getChildAt(adapter.getPosition(str))
                                .setBackgroundColor(
                                        getResources()
                                                .getColor(
                                                        android.R.color.darker_gray));
                    } 
                }
            }
        }
    });

Upvotes: 1

Blaze Tama
Blaze Tama

Reputation: 10948

You can create a new List, with the (i assume you have) id like the listview's data.

Example :

You have class Person with int id, String name. Create a new List<Integer> to store all (not just the visible one) of your listview's person id, normally by using the listview's adapter.

Upvotes: 0

Related Questions