在会议室Android Android Studio中填充一个expandableListView

时间:2018-06-25 22:50:06

标签: android android-studio android-room

我正在尝试填充多级ExpandableListView(3个级别)。 目前,我只能显示完整的第一级(标题),但只能显示第二级的第一项,而不能显示第三级

ExpListView是 layout_list_view

我的数据库数据是:example data of the database

我对数据库进行了三个选择, 1:日期; 2:按日期划分的位置; 3:按位置和日期划分的一般数据

@Query("SELECT fecha_inventario FROM lotes GROUP BY fecha_inventario")
    public List<String> getFechasLotes();

@Query("SELECT desc_ubicacion_lote FROM lotes WHERE fecha_inventario = :fecha GROUP BY desc_ubicacion_lote ORDER BY desc_ubicacion_lote ASC")
    public List<String> getUbicacionesLotesByFecha(String fecha);

@Query("SELECT lote FROM lotes WHERE fecha_inventario = :fecha AND desc_ubicacion_lote = :ubicacion ORDER BY fecha_inventario DESC")
    public List<String> getLotesByFechaAndUbicacion(String fecha, String ubicacion);  

我正在尝试在称为HomeFragment的Fragment中显示所有内容。我将粘贴片段中的完整代码

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.Toast;

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

import cl.zcloud.www.inventariolotes.MainActivity;
import cl.zcloud.www.inventariolotes.R;
import cl.zcloud.www.inventariolotes.adapters.lotes.AdaptadorListaPrimerNivel;
import cl.zcloud.www.inventariolotes.clases.Lotes;

public class listarRegistrosFragment extends Fragment {

    ExpandableListView expandableListView;
    AdaptadorListaPrimerNivel listAdapter;
    ArrayList<String> listDataHeader;
    ArrayList<String> listDataUbicacion;
    HashMap<String, List<String>> listDataChild;


//    List<String[]> secondLevel = new ArrayList<>();

//    List<LinkedHashMap<String, String[]>> data = new ArrayList<>();



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.listar_registros_fragment, container, false);

        //lista expandible
        expandableListView = view.findViewById(R.id.expandable_lv);

        //cargar arrays;
        prepareListData();

        // parent adapter
//        ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(getActivity(), parent, secondLevel, data);

        listAdapter = new AdaptadorListaPrimerNivel(getActivity(), listDataHeader, listDataUbicacion,listDataChild);

        // set adapter
        expandableListView.setAdapter( listAdapter );





        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getActivity(), "click en child", Toast.LENGTH_SHORT).show();
                return false;
            }

        });

        expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP){
                    int groupPosition = ExpandableListView.getPackedPositionGroup(id);
//                    int childPosition = ExpandableListView.getPackedPositionChild(id);

                    Toast.makeText(getActivity(), "click largo en parent", Toast.LENGTH_SHORT).show();

                    return true;
                }
                return false;
            }
        });


        return view;
    }


    private void prepareListData() {

        listDataHeader = new ArrayList<>();
        List<String> listaFechas = MainActivity.myAppDB.myDao().getFechasLotes();

        List<String> listaLotes = null;
        if (listaFechas.size() > 0){

            listDataHeader.addAll(listaFechas);

            System.out.println("SIZE DE LISTA FECHA  = " + listaFechas.size());
            for (String fechas : listaFechas){

                List<String> listaUbicaciones = MainActivity.myAppDB.myDao().getUbicacionesLotesByFecha(fechas);

                if (listaUbicaciones.size() > 0){
                    System.out.println("SIZE DE LISTA UBICACION  = " + listaUbicaciones.size());

                    listDataUbicacion = new ArrayList<>();
                    listDataUbicacion.addAll(listaUbicaciones);

                    listDataChild = new HashMap<>();
                    for (String ubicaciones : listaUbicaciones){


                        System.out.println("  ");
                        System.out.println("--------EMPIEZA--------");
                        System.out.println("--------PRIMER NIVEL--------");
                        System.out.println(fechas);


                        System.out.println("-----------SEGUNDO NIVEL--------------");
                        System.out.println(ubicaciones);


                        System.out.println("-----------TERCER NIVEL--------------");
                        listaLotes = MainActivity.myAppDB.myDao().getLotesByFechaAndUbicacion(fechas, ubicaciones);
                        System.out.println(listaLotes);

                        for (int  e = 0 ; e < listaLotes.size(); e++){
//                            System.out.println(listaLotes.get(i));
                            listDataChild.put(ubicaciones, listaLotes);
                        }
                    }
//                    if (listaLotes != null && listaLotes.size() > 0){
//
//                    }


                   /* for (String ubicacion : listaUbicaciones){



                    }*/
                }

            }
        }


/*        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("The Shawshank Redemption");
        top250.add("The Godfather");
        top250.add("The Godfather: Part II");
        top250.add("Pulp Fiction");
        top250.add("The Good, the Bad and the Ugly");
        top250.add("The Dark Knight");
        top250.add("12 Angry Men");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("The Conjuring");
        nowShowing.add("Despicable Me 2");
        nowShowing.add("Turbo");
        nowShowing.add("Grown Ups 2");
        nowShowing.add("Red 2");
        nowShowing.add("The Wolverine");

        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
        comingSoon.add("The Smurfs 2");
        comingSoon.add("The Spectacular Now");
        comingSoon.add("The Canyons");
        comingSoon.add("Europa Report");


        for (int i = 0; i < listDataHeader.size(); i++){
            Lotes lotes = new Lotes();
            peliculas.setNombre(listDataHeader.get(i));
            peliculas.setPeliculasList(top250);
            listDataChild.put(peliculas.getNombre(), peliculas.getPeliculasList()); // Header, Child data
        }*/
    }
}

制作三个适配器

public class AdaptadorListaPrimerNivel extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private List<String> _listSecondLevel;
    private HashMap<String, List<String>> _listThirdLevel;


    public AdaptadorListaPrimerNivel(Context context,
                                     List<String> listDataHeader,
                                     List<String> listChildData,
                                     HashMap<String, List<String>>_listThirdLevel) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listSecondLevel = listChildData;
        this._listThirdLevel = _listThirdLevel;
    }

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

    @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 SecondLevelExpandableListView adapterLevel2 = new SecondLevelExpandableListView(_context);

//        List<String> dataTercerLevel = new ArrayList<>();
//        HashMap<String, List<String>> secondLevelData= _listThirdLevel.get(childPosition);

        adapterLevel2.setAdapter(new AdaptadorListaSegundoNivel(_context, _listSecondLevel , _listThirdLevel));
        adapterLevel2.setGroupIndicator(null);


        adapterLevel2.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            int previousGroup = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                if(groupPosition != previousGroup)
                    adapterLevel2.collapseGroup(previousGroup);
                previousGroup = groupPosition;
            }
        });



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

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

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

        txtListChild.setText(childText);*/
        return  adapterLevel2;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listSecondLevel.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) {

        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_groups, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

第二

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

public class SecondLevelExpandableListView extends ExpandableListView {
    public SecondLevelExpandableListView(Context context) {
        super(context);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

AND

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

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

import cl.zcloud.www.inventariolotes.R;

public class AdaptadorListaSegundoNivel extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _headers;
    private HashMap<String, List<String>> _child;

    public AdaptadorListaSegundoNivel(Context context, List<String> headers, HashMap<String, List<String>> child){
        this._context = context;
        this._headers = headers;
        this._child = child;

    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (inflater != null) {
            convertView = inflater.inflate(R.layout.row_second, null);
        }
        TextView text = convertView.findViewById(R.id.lblListFecha);
        String groupText = this._headers.get(groupPosition);
        text.setText(groupText);

        return convertView;
    }

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

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

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (inflater != null) {
            convertView = inflater.inflate(R.layout.row_third, null);
        }

        TextView textView =  convertView.findViewById(R.id.lblListUbicacion);

        String childArray = (String) getChild(groupPosition, childPosition);

        textView.setText(childArray);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._child.size();
    }

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

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

我不知道该如何解决该问题,因为它没有向我显示错误,请帮助我。谢谢

0 个答案:

没有答案