Reputation: 35
I have an ArrayList filled with Integers and a Listview. Now i wanna got through this Arraylist, and if status.get(i) % 2 != 0
, i wanna strike through the according child of a ListView filled with TextViews.
My code:
for(int i = 0; i < mLsitView.getCount(); i++){
TextView tv = (TextView) mLsitView.getChildAt(i);
if(mArrayList.get(i) % 2 != 0) {
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
}
When i perform this code, its giving me a NullPointerException, telling me: Attempt to invoke virtual method 'Attempt to invoke virtual method 'int android.widget.TextView.getPaintFlags()' on a null object reference
.
I've already searched the internet, but i couldn't find a solution. Somebody said you should use for (int 1 = 0; i < mLsitView.getFirstVisiblePosition() - mLsitView.getLastVisiblePosition(); i++
in the loop, but it just returns 0 for me and the piece of code doesnt get performed.
Upvotes: 0
Views: 91
Reputation: 2177
ListViews are different from regular Views in that the children of a ListView don't all exist at once. ListViews generate just enough child Views to fit on the screen of the device, plus a few extra. As you scroll through the ListView, the children Views that disappear off the top of the screen are recycled and placed at the bottom again for reuse so that the phone doesn't have to constantly be creating and destroying Views.
To put a strike-through on every other View, you have to create a custom Adapter. When your Adapter is called, it also receives the position of the View you're creating and you can use that to determine whether or not to paint the strike-through.
Upvotes: 0
Reputation: 11642
You can't do like that because list will not create all the view in one shot,
You can solve this problem by put you condition in getView
in your adapter.
Upvotes: 2