Android: Access Child Views from a Listview

Android: Access child views from a ListView

See: Android ListView: get data index of visible item
and combine with part of Feet's answer above, can give you something like:

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);

The benefit is that you aren't iterating over the ListView's children, which could take a performance hit.

Android Get child items in listView

Don't retrieve these views. Pass checked/unchecked information in your onCheckedChangeListener - e.g. String and info if it is checked/unchecked. Then you can collect checked items in array.

Try to do something like that:

public class TestClass {

private List<String> checkedStrings = new ArrayList<>();

...

void updateArray(boolean checked, String checkedText){
if(checked){
if(!checkedStrings.contains(checkedText)){
checkedStrings.add(checkedText);
}
} else {
checkedStrings.remove(checkedText);
}
}
}

And in your ArrayAdapter:

appCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
updateArray(isChecked, apps.get(position).label);
}
});

How to get a ListView's child view at specific index

I figured out how to do this.

I really think that one should not have to post code and logcat for a question that is similar to "How to set background image".

Anyway my answer is, in short, set position as tag for every child in the listview through your adapter's getView() and then get any child using findViewWithTag(Object tag).

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout)inflater.inflate(R.layout.some_layout, parent, false); .
convertView.setTag(String.valueOf(position));
}

And whenever you need to get a specific child.

View v = mylistview.findViewWithTag(String.valueOf(index));

To change background color.

v.setBackgroundResource(R.color.some_color);

How to get child views of listview item from outside the adapter

I am assuming that you show this layout in a ListView or RecyclerView. First thing that need to do is, add a field for index in your Download class to identify the position in the list from where that button was pressed. Next, pass the index from your getView() or onBindViewHolder() to the function that creates the Download object and add that index to the object.

When you receive the when you broadcast any update from your Service, do include the index value. Now, in your code in your activity where you receive the broadcast value, extract the value of index and the progress. Now, you could write something like this to update the progress of that view in the list:

int index = ... //some value you received in broadcast
int progress = ...//some value you received in broadcast

View view = listView.getChildAt(index);
if (view != null) {
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
if (progressBar != null) {
progressBar.setProgress(progress);
}
}

Access Child View from List Adapter

I got round this by doing the following:

  • I marked the item I wished to select in the adapter first by adding in my own method
  • Then when the view is requested I can check to see if the view is for a position at which I requested
  • I get then modify the presented view that is returned by GetView

    This seems the simplest option.

How to get all children (visible and invisible) from a ListView?

As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.

But the ListView doesn't keep the current values from RadioGroup in
running time.

I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.

Android - get children inside a View?


for(int index = 0; index < ((ViewGroup) viewGroup).getChildCount(); index++) {
View nextChild = ((ViewGroup) viewGroup).getChildAt(index);
}

Will that do?

Access Child Item's individual ojects in Expandable Listview

You need to make a class to extend the BaseExpandableListAdapter, and then override the abstract getChildView() method, this method is based on groupPosition and childPosition to determine which view to be displayed. You can also visit this Expandable List View tutorial.

In the tutorial, it shows how to access the TextView object in each child view, if you want to access the CheckBox, you can do the similar thing. Below is a sample code:

    @Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
CheckBox checkboxListChild = (CheckBox) convertView
.findViewById(R.id.checkboxListItem); //assume thats the id of your checkbox object


txtListChild.setText(childText);
return convertView;
}

You can also check out this tutorial to learn about ExpandableListView with checkbox.



Related Topics



Leave a reply



Submit