javaandroidandroid-listviewandroid-identifiers

Android - findViewWtihTag returns null


The findViewWithTag in this activity is returning null, but the Log in the CustomExpandableListAdapter outputs correctly. What am I doing wrong here?

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

//normal stuff

public View getGroupView(int groupPosition, boolean isLastChild,
View view, ViewGroup parent) {

    CustomObject group = (CustomObject) getGroup(groupPosition);
    //normal stuff

    LinearLayout toggle = (LinearLayout) view.findViewById(R.id.toggle);
//group.getId() returns an int
    toggle.setTag("toggle" + group.getId());
    Log.i("BBBBBtoggleadapterBBBBBB", toggle.getTag().toString());


    return view;
}


}



public class MainActivity extends BaseListActivity {

//normal stuff

//this is an xml onClick
public void toggleView(View view) {

  String groupId = view.getTag().toString();
  //LinearLayout toggle = (LinearLayout) view.findViewWithTag("toggle " + groupId);
   Log.i("BBBBBBBBBtoggleactivityBBBBBBBBBB", "toggle" + groupId);

    /*if (toggle == null) {
    Toast.makeText(getBaseContext(), "null", Toast.LENGTH_LONG).show();
    }*/

}

}

edit:updated code, Logged "toggle" + id in activity and adapter and they are identical


Solution

  • I figured it out. findViewWithTag looks for child tags that match. The LinearLayout I was trying to identify is not a child of the button who's onClick triggered toggleView. So, I needed to go up (2 levels, in this case) to find it.

    View parent = (View) view.getParent().getParent();
    LinearLayout toggle = (LinearLayout) parent.findViewWithTag("toggle" + groupId);