【问题标题】:How to settext TextView in group in ExpandableListView [duplicate]如何在 ExpandableListView 中的组中设置文本 TextView [重复]
【发布时间】:2016-02-22 05:43:21
【问题描述】:

我想要做的是,当我单击 ExpandableListView 中的子项时,它将根据选定的子项更改父项(groupView)。简而言之,这就像 HTML 中的 <select><option/></select>

这是子视图

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


    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater= (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("Onclick CHild: ", childText);

            //This is where I want to setText the View of its groupView
        }
    });

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.listItem);

    txtListChild.setText(childText);
    return convertView;
}

这是组视图

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch(groupPosition){
            case 0:{
                convertView = infalInflater.inflate(R.layout.type_header, null);
                break;
            }
            case 1:{
                convertView = infalInflater.inflate(R.layout.make_header, null);
                break;
            }
            case 2:{
                convertView = infalInflater.inflate(R.layout.model_header, null);
                break;
            }
            case 3:{
                convertView = infalInflater.inflate(R.layout.engine_header, null);
                break;
            }
            case 4:{
                convertView = infalInflater.inflate(R.layout.weight_header, null);
                break;
            }
        }
    }


    Holder holder = new Holder(this._context);
    holder.title = (TextView) convertView.findViewById(R.id.headerTitle);
    holder.title.setText(headerTitle);

    return convertView;
}

【问题讨论】:

    标签: java android expandablelistview expandablelistadapter


    【解决方案1】:

    public View getGroupView(int groupPosition, boolean isExpanded,View convertView,ViewGroup parent) { String headerTitle = (String) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch(groupPosition){
            case 0:{
                convertView = infalInflater.inflate(R.layout.type_header, null);
                break;
            }
            case 1:{
                convertView = infalInflater.inflate(R.layout.make_header, null);
                break;
            }
            case 2:{
                convertView = infalInflater.inflate(R.layout.model_header, null);
                break;
            }
            case 3:{
                convertView = infalInflater.inflate(R.layout.engine_header, null);
                break;
            }
            case 4:{
                convertView = infalInflater.inflate(R.layout.weight_header, null);
                break;
            }
        }
    }
    Holder holder = new Holder(this._context);
    holder.title = (TextView) convertView.findViewById(R.id.headerTitle);
    holder.title.setText(getGroup(groupPosition));
    return convertView;}
    

    请使用此代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      相关资源
      最近更新 更多