1

这是我的第一个问题,我需要动态添加textViews到可扩展方法convertView中返回的那个getChildView(),但我不能以任何方式做..

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

        ViewHolderChild holder;
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_line, null);
            holder = new ViewHolderChild();
            holder.txNumeroSolicitacao = (TextView) convertView.findViewById(R.id.textViewNumeroSolicitacao);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderChild) convertView.getTag();
        }
        holder.txNumeroSolicitacao.setText(String.valueOf(getChild(groupPosition,childPosition).getNumero()));


    //if my log list isn't empty, I want add a textview dynamically to view     
        if(!getChild(groupPosition,childPosition).getLog().isEmpty()) {

            holder.textViewResponse = new TextView(context);
            RelativeLayout.LayoutParams alignParentLeft = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
            alignParentLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            holder.textViewResponse.setLayoutParams(alignParentLeft);
            ((ViewGroup) convertView).addView(holder.textViewResponse);
        }
        return convertView;
    }

但我不能添加textViewconvertView. 应用程序总是返回意外行为。

4

1 回答 1

0

您可以在 child_line.xml 中添加 TextView 并使用 android:visibility="gone",并根据当前行的绑定对象的日志是否为空来设置它在 getChildView 中的可见性。

于 2014-04-24T14:03:35.687 回答