getChildAt return a View that is null - android

I try to animate a single item in my ListView, I've figured out that this is kinda hard. But, after some googling I came up with some codesnippets that I combined, but when I try to call my ListView's getChildAt in order to return the View, this returns null.
public void animateSingleItemInListView() {
final Animation animBounce = AnimationUtils.loadAnimation(context, R.anim.bounce);
int totalItemsInListView = lastCases.getCount();
int wantedPosition = 1; // Whatever position you're looking for
int firstPosition = lastCases.getFirstVisiblePosition() - lastCases.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
int childs = lastCases.getChildCount();
if (wantedChild < 0) {
Log.w("UPDATEUI", "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
// Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
View wantedView = lastCases.getChildAt(firstPosition);
wantedView.setAnimation(animBounce);
}
From the code:
The totalItemsInListView returns 10, which is correct number of rows in my ListView. The integer firstPosition is of course 0, but the getChildAt() method only returns null. How come? How can I get the first View from my ListView?

try to use
lastCases.getItemAtPosition(firstPosition)

Related

How to get the actual index of a child in a ListView?

I am currently using the getChildAt method for getting the child rows inside a ListView. The problem with this is that it only gets the child correctly if the ListView is scrolled all the way to the top.
If I were to scroll down a bit so that the first row is hidden and call the getChildAt method for position 1 it would get the view that is third in the ListView.
Is there a way to get the child view with an actual position rather than the position visible on the screen? So that in the case above the getChildAt would still return a view (2nd child) even if it isn't visible on the screen.
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}}
reference: android - listview get item view by position

get a subView inside a ListView's row layout

I need to change the drawable resource used in a ImageView inside a ListView's row.
I'm using this:
int wantedPosition = 10; // Whatever position you're looking for
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
View wantedView = listView.getChildAt(wantedChild);
if I search with findViewById inside wantedView, I don't find the ImageView that I'm looking for.
How can I refer this ImageView to set another drawable resource?
fast way: use setTag(position) (or getItem or whatever you want to set as a tag) for your View in your adapter (getView method or relative) and further use listView.findViewByTag

get imageview from row listview (android)

how can I access a single imageview which is located inside a row of a listview?
I have a listview that contains many rows custom xml. each row contains a imageview and a TextView. Once inserted these lines in listview how can I change the bitmap of an imageview to a specific row?
Assuming you know which position you want to update and you're on the UI thread, you can use
public View getViewForPosition(int position){
int relativePos = position - listview.getFirstVisiblePosition();
if( relativePos < 0 || relativePos > listview.getChildCount()){
return null;
}
return listview.getChildAt(relativePos);
}
A return of null from this function means that position is offscreen. A non null will return the view from getView, and you can then do a findViewById on it to find the child you want.
If calling from a non-UI thread, you can be off by 1 due to calculating as the view mapping is changing. There is a hack to fix this I came up with once, but I'd suggest just not doing it.
Edit: here's the thread safe hack.
public View getViewForPosition(int position){
int relativePos = position - listview.getFirstVisiblePosition();
//Hack for allowing us to get a view for a position that is currently being created
if( (relativePos == listview.getChildCount() || relativePos == -1) && currentProcessingView != null){
return currentProcessingView;
}
if( relativePos < 0 || relativePos >= listview.getChildCount()){
return null;
}
return listview.getChildAt(relativePos);
}
currentProcessingView should be set in getView to the current position's view at the very beginning, and to null at the end of getView. If the position is not currently on screen it will return null, you need to be able to handle that.
You could uniquely tag each of the listview items and then use findViewWithTag on it.

how to get data from different items in a listview in android

I am working on list view in android where i have placed edit text on each item of a list view now I want to select some items of that list view and want to get data of the selected ones only ,I means items where I have filled edit text.
I am using list adapter to get data into the list view, now suggest me something if you got what I mean.
From Android: Access child views from a ListView:
int wantedPosition = 10; // Whatever position you're looking for
int firstPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= listView.getChildCount()) {
Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
return;
}
// Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead.
View wantedView = listView.getChildAt(wantedChild);
And then:
EditText yourEditText = (EditText)wantedView.findViewById(R.id.yourEditTextId);

Get all full visible objects on lListView

I have a ListView, which contains more elements then I can display at one time.Now I want to get Index off all Elements, which are full visible ( -> excluding those that are only partially visible).
At this moment I use getFirstVisiblePosition() & getLastVisiblePosition() into an for-loop to iterate them, but these method is not accurate as I want to.
Is there any better solution?
A ListView keeps its rows organized in a top-down list, which you can access with getChildAt(). So what you want is quite simple. Let's get the first and last Views, then check if they are completely visible or not:
// getTop() and getBottom() are relative to the ListView,
// so if getTop() is negative, it is not fully visible
int first = 0;
if(listView.getChildAt(first).getTop() < 0)
first++;
int last = listView.getChildCount() - 1;
if(listView.getChildAt(last).getBottom() > listView.getHeight())
last--;
// Now loop through your rows
for( ; first <= last; first++) {
// Do something
View row = listView.getChildAt(first);
}
Addition
Now I want to get Index off all Elements, which are full visible
I'm not certain what that sentence means. If the code above isn't the index you wanted you can use:
int first = listView.getFirstVisiblePosition();
if(listView.getChildAt(0).getTop() < 0)
first++;
To have an index that is relative to your adapter (i.e. adapter.getItem(first).)
The way I would do this is I would extend whatever view your are passing in getView of the ListView adapter and override the methods onAttachedToWindow and onDetachedToWindow to keep track of the indexes that are visible.
Try onScrollListner and you can able to use getFirstVisiblePosition and getLastVisiblePosition.
This this link, it contain similar type of problem. I suppose you got your answer there..,.
The above code is somewhat correct. If you need to find the completely visible location of view use below code
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
View v = null;
if (scrollState == 0) {
int first =0;
if (view.getChildAt(first).getTop() < 0)
first++;
int last = list.getChildCount() - 1;
if (list.getChildAt(last).getBottom() > list
.getHeight())
last--;
// Now loop through your rows
for ( ; first <= last; first++) {
// Do something
View row = view.getChildAt(first);
// postion for your row............
int i=list.getPositionForView(row);
}
}
// set the margin.
}

Categories

Resources