Добавить представление, созданное динамически, чтобы преобразовать представление в расширяемый список просмотра Android

Это мой первый вопрос, мне нужно добавить динамически 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;
    }

Но я не могу добавить textView в convertView, Приложение всегда возвращает неожиданное поведение.

1 ответ

Вы можете добавить TextView в child_line.xml с помощью android:visibility="пошел" и установить его видимость в getChildView в соответствии с тем, пуст ли журнал привязанного объекта текущей строки.

Другие вопросы по тегам