How to capture the click on a sub-item of an Expandable List View

Asked

Viewed 87 times

0

So personally, I’m completing my t.i. course at Nai and I’m developing a TCC for the course. My TCC consists of an app of schedules of the bus lines of my city. For this I decided to create an Expandable list view learning by a video on youtube, only, I’m not able to "click" on the sub-item of the list (clicked on the desired neighborhood, opened the routes and the user would select which one he wanted, by his choice would open an Activity with the respective schedules of the chosen route), so he needed help from any of you <3.

This is the java folder expandableListAdapter

package com.example.sesi2018.busu.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.sesi2018.busu.R;

import java.util.HashMap;
import java.util.List;

/**
 * Created by sesi2018 on 26/04/2018.
 */

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> listHashMap;


    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listHashMap = listHashMap;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return listHashMap.get(listDataHeader.get(groupPosition)).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
      String headerTitle = (String)getGroup(groupPosition);
        if (convertView == null ) {
            LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_group,null);
        }
        TextView lblListHeader = (TextView)convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, 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);
        }

        TextView txtListChild = (TextView)convertView.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);
        return convertView;
    }

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

This is the expandableListView folder

package com.example.sesi2018.busu.Activity;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;

import com.example.sesi2018.busu.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Turi extends AppCompatActivity {

    private ExpandableListView listView;
    private  ExpandableListAdapter listAdapter;
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listHash;


    private ArrayAdapter<String> horariosadap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turi);

        listView = (ExpandableListView) findViewById(R.id.lvExp);

        initData();
        listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
        listView.setAdapter(listAdapter);




   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle("Linhas - Turi");



        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int posicaoClick = position;
                String conteudoClick = (String) listView.getItemAtPosition(posicaoClick);
                if(conteudoClick.equals("Isso é uma Expandable ListView")){

                    Intent bla2 = new Intent(Turi.this, MainActivity.class);
                    startActivity(bla2);

                }
            }
        });






    }

    private void initData() {
        listDataHeader = new ArrayList<>();
        listHash = new HashMap<>();

        listDataHeader.add("Alvorada");
        listDataHeader.add("Barreiro");
        listDataHeader.add("Bela Vista");
        listDataHeader.add("Catavento");

        List<String> lista1 = new ArrayList<>();
        lista1.add("Alvorada -> Centro");
        lista1.add("Alvorada -> Shopping");

        List<String> lista2 = new ArrayList<>();
        lista2.add("1");
        lista2.add("2");
        lista2.add("3");


        List<String> lista3 = new ArrayList<>();
        lista3.add("33");
        lista3.add("22");


        List<String> lista4 = new ArrayList<>();
        lista4.add("5");
        lista4.add("4");
        lista4.add("3");
        lista4.add("2");
        lista4.add("1");

        listHash.put(listDataHeader.get(0),lista1);
        listHash.put(listDataHeader.get(1),lista2);
        listHash.put(listDataHeader.get(2),lista3);
        listHash.put(listDataHeader.get(3),lista4);


    }




    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case android.R.id.home:
                startActivity(new Intent(Turi.this,Horarios.class));
                finishAffinity();
                break;
            default:
                break;
        }
        return true;
    }


}

this was the video I watched to make the expandablelist

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.