0

此活动中的 findViewWithTag 返回 null,但 CustomExpandableListAdapter 中的 Log 输出正确。我在这里做错了什么?

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();
    }*/

}

}

编辑:更新代码,在活动和适配器中记录“切换”+ id,它们是相同的

4

2 回答 2

2

我想到了。findViewWithTag 查找匹配的子标签。我试图识别的 LinearLayout 不是 onClick 触发的 toggleView 按钮的子级。所以,我需要上去(在这种情况下是 2 个级别)才能找到它。

View parent = (View) view.getParent().getParent();
LinearLayout toggle = (LinearLayout) parent.findViewWithTag("toggle" + groupId);
于 2013-12-14T18:07:35.750 回答
0

你的标签不一样。 "id " + group.getId()!="toggle " + groupId

于 2013-12-12T18:18:41.103 回答