ExpandableListView: change the header title when clicking on an item in the list

0

I need to get the name of the items from the ExpandableListView list, and settar in the header according to his position, but he is only getting the last one. Here is my code below:

MainActivity

    public class MainActivity extends AppCompatActivity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get the listview
        expListView = findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        //  PEGA A POSIÇÃO DO ITEM DA LISTA
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Log.i("LOG", "Posição: " + childPosition);

                parent.collapseGroup(0);

                return false;
            }
        });

        //  TROCA O ICONE DE EXPANSAO
        expListView.setGroupIndicator(getResources().getDrawable(R.drawable.ic_launcher_background));
    }

    private void prepareListData() {
        listDataHeader = new ArrayList<>();
        listDataChild = new HashMap<>();

        // Adding child data
        listDataHeader.add("Lista de itens");

        // Adding child data
        List<String> itens = new ArrayList<String>();
        itens.add("Item 1");
        itens.add("Item 2");
        itens.add("Item 3");
        itens.add("Item 4");
        itens.add("Item 5");

        listDataChild.put(listDataHeader.get(0), itens); // Header, Child data

    }
}

ExpandableListAdapter

    public class ExpandableListAdapter extends BaseExpandableListAdapter implements IAtualizaNome {
    String headerTitle;
    private Context ctx;
    private List<String> listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> listDataChild;
    String name;

    public ExpandableListAdapter(Context ctx, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
        this.ctx = ctx;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    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 infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_lista, null);
        }

        TextView tvItemLista = convertView.findViewById(R.id.tvw_item_nome);
        tvItemLista.setText(childText);

        name = childText;

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView tvTitleHeader = convertView.findViewById(R.id.tvw_title_header);
        tvTitleHeader.setTypeface(null, Typeface.BOLD);

        headerTitle = name;

        tvTitleHeader.setText((headerTitle == null) ? "Selecione" : headerTitle);

        return convertView;
    }

    public void changeHeader(String text) {
        headerTitle = text;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void updateName(String name) {
        this.name = name;
    }
}
    
asked by anonymous 22.02.2018 / 15:40

1 answer

0

Solved! It was a problem with hierarchy. Now my code looks like this:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    String headerTitle;
    private Context ctx;
    private List<String> listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> listDataChild;
    TextView tvTitleHeader;

    public ExpandableListAdapter(Context ctx, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
        this.ctx = ctx;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        tvTitleHeader.setText((headerTitle == null) ? "Selecione" : listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition));
        return childPosition;
    }

    @Override
    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 infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_lista, null);
        }

        TextView tvItemLista = convertView.findViewById(R.id.tvw_item_nome);
        tvItemLista.setText(childText);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        tvTitleHeader = convertView.findViewById(R.id.tvw_title_header);
        tvTitleHeader.setTypeface(null, Typeface.BOLD);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
    
22.02.2018 / 19:28