【问题标题】:set Tag to Every Child in expandable list将标签设置为可扩展列表中的每个孩子
【发布时间】:2013-09-25 06:38:09
【问题描述】:

我在我的项目中使用可扩展列表。我扩展了 BaseExpandableListAdapter,我想为每个孩子设置唯一标签,我使用以下代码设置标签:

     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ArrayList<String> childtemp= (ArrayList<String>) childitems.get(groupPosition);


    final ViewHolder holder;
    View v = convertView;

    if (v == null) {
        holder = new ViewHolder();
        v = inflatter.inflate(R.layout.childrow, null);
        holder.text = (TextView) v.findViewById(R.id.childrow);
        holder.checked = (CheckBox) v.findViewById(R.id.childcheck);
        v.setTag(holder);

        holder.checked.setChecked(false);
        holder.checked.setTag(groupPosition*100+childPosition);
        holder.text.setTag(groupPosition*100+childPosition);
     }
     else {
        ((ViewHolder) v.getTag()).text.setTag(groupPosition*100+childPosition);
        holder = (ViewHolder) v.getTag();
        getid = (Integer) holder.text.getTag();
        check(getid , holder);  
        }

     holder.text.setText(childtemp.get(childPosition).toString());

它适用于我展开的第一个位置,但是当我展开另一个未设置的位置标记时,因为 convertView 不为空。如何为每个孩子设置标签做一些事情?我覆盖 onGroupExpanded 但我不知道如何使用它。

【问题讨论】:

    标签: android listview expandablelistview


    【解决方案1】:
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        List<String> childtemp= (List<String>) childitems.get(groupPosition);
    
        final ViewHolder holder;
        View v = convertView;
    
        if (v == null) {
            holder = new ViewHolder();
            v = inflatter.inflate(R.layout.childrow, null);
            holder.text = (TextView) v.findViewById(R.id.childrow);
            holder.checked = (CheckBox) v.findViewById(R.id.childcheck);
            v.setTag(holder);
    
            holder.checked.setChecked(false);
         }
         else {
            ((ViewHolder) v.getTag()).text.setTag(groupPosition*100+childPosition);
            holder = (ViewHolder) v.getTag();
            getid = (Integer) holder.text.getTag();
            check(getid , holder);  
         }
    
         holder.checked.setTag(groupPosition*100+childPosition);
         holder.text.setTag(groupPosition*100+childPosition);
         holder.text.setText(childtemp.get(childPosition).toString());
    

    【讨论】:

    • sir @Autocrab,.....我在设置标签时收到类似“密钥必须是特定于应用程序的资源 ID”的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多