Domanda

When I select one of the items from the listview that is not visible when the activity is created it throws an exception becuase those that are not visible are null. As you see, I already know why the problem gives the exception and I would thank the one that gives me the tip for solving it. Here's the code.

public void myFunction(View view) {
        int i, i2 = 0;
        // get the row the clicked button is in
        ListView lerroa = (ListView) view.getParent();
        i = lerroa.getPositionForView(view);
        System.out.println(i + 1);


        LinearLayout ll = (LinearLayout) lerroa.getChildAt(i);
        TextView pedido = (TextView) ll.getChildAt(0);
}

Any easier way of taking the textview at the item that has been clicked on?

È stato utile?

Soluzione

I've solved the problem.

In order to show also the items of the list view that were not visible, those that caused the NullPointers, I've replaced the line of code which crashed the app. Here it is:

Replace:

LinearLayout ll = (LinearLayout) lerroa.getChildAt(i);

For this one:

LinearLayout ll = (LinearLayout) lerroa.getChildAt(i -lerroa.getFirstVisiblePosition()).findViewById(R.id.LinearLayout3);

getVisiblePosition allows us playing with the first item shown at the LinearLayout and so avoids any NullPointer.

I know this is a bit shoddy and that is not the best way to code what I want to achieve. In spite of this, I've seen so many times the same error on the internet but without any correct answare. I wish this would be usefull for others.

Have a nice code!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top