无法在ExpandableListView中为Viewholder设置标签和getTag

时间:2016-03-01 05:27:56

标签: java android expandablelistview android-viewholder

我正在开发一个可扩展列表,该列表在不同的子项中具有不同的布局。我也使用viewHolder方法来缓存视图。该应用程序崩溃时出现此错误:

java.lang.IllegalArgumentException: The key must be an application-specific resource id.

我知道它需要类似布局ID的关键字,但在这种情况下,viewHolder的唯一方法就是childType

int childType = getChildType(groupPosition, childPosition);

if (convertView == null || convertView.getTag(childType) != null) {
   viewHolder = new ViewHolder();
   convertView = inflater.inflate(R.layout.list_item, null);
   viewHolder.text = (TextView)convertView.findViewById(R.id.text);
   convertView.setTag(childType,viewHolder);

}else{

   viewHolder = (ViewHolder) convertView.getTag(childType);
}

if(childType == 0){
    /***Render Stuff***/
}

如何使用childType为不同类型的子项调用viewHolder?

完整代码:

public class ExpandableListAdapter extends BaseExpandableListAdapter{

    private Context context;

    private Map<String, List<?>> dataCollections;

    public ExpandableListAdapter(FragmentActivity context, List<String> laptops,
            Map<String, List<?>> dataCollections) {
        this.context = context;

        this.dataCollections = dataCollections;

    }


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

        MappingItem row = (MappingItem) getChild(groupPosition, childPosition);  

        int childType = getChildType(groupPosition, childPosition);

        ViewHolder viewHolder;

        if (convertView == null || convertView.getTag(childType) != null) {

            viewHolder = new ViewHolder();

            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            viewHolder = new ViewHolder();
            if(childType == 0){

               convertView = inflater.inflate(R.layout.list_item, null);

               viewHolder.text = (TextView)convertView.findViewById(R.id.text);

            }

            convertView.setTag(childType,viewHolder);
                                 ^^^^^^
        }else {

            viewHolder = (ViewHolder) convertView.getTag(childType);

        }

        if(childType == 0){

               /********Render Something**********/

        }

        return convertView;
    }


}

0 个答案:

没有答案