0

I'm having issues getting an item from a ListView when it is off screen. If I scroll down and hit next, the top item returns null. If I scroll back up, the bottom item return null.

I know ListView dynamically creates the view so is it possible to get items off screen?

public void onNextClick(View w){
    Intent i = new Intent(this, homeActivity.class);

        for(int position = 0; position < expandableLayoutListView.getAdapter().getCount(); position++)
        {

            try {
                Hub temp = ((Hub) expandableLayoutListView.getAdapter().getItem(position));
                System.out.println("Position " + position);
                System.out.println("Other position " + (position - expandableLayoutListView.getFirstVisiblePosition()));

                ExpandableLayoutItem expandableLayout = (ExpandableLayoutItem)
                        expandableLayoutListView.getChildAt(position - expandableLayoutListView.getFirstVisiblePosition())
                                .findViewWithTag(ExpandableLayoutItem.class.getName());


                JoinedHub hub = new JoinedHub(temp.name);

                if(((CheckBox) expandableLayout.findViewById(R.id.checkBox1)).isChecked())
                    hub.addToSubHubs( ((CheckBox) expandableLayout.findViewById(R.id.checkBox1)).getText().toString()  );
                if(((CheckBox) expandableLayout.findViewById(R.id.checkBox2)).isChecked())
                    hub.addToSubHubs(((CheckBox) expandableLayout.findViewById(R.id.checkBox2)).getText().toString());
                if(((CheckBox) expandableLayout.findViewById(R.id.checkBox3)).isChecked())
                    hub.addToSubHubs(((CheckBox) expandableLayout.findViewById(R.id.checkBox3)).getText().toString());

                Log.d("Debugging position", temp.getName() + " " + position);

                if(!hub.subHubs.isEmpty()){
                    joinedHubs.add(hub);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    Log.d("Debugginggg", joinedHubs.toString());
    startActivity(i);

}
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119

1 Answers1

0

You can not get items that are off screen, they simply do not exist (for performance reasons, these views get re-used to show other items).

This question might help you to achieve what you want to get.

Community
  • 1
  • 1
ByteHamster
  • 4,884
  • 9
  • 38
  • 53