如何使用arraylist制作可扩展列表?

时间:2017-11-23 10:53:41

标签: java android expandablelistview recycler-adapter

我需要制作一个可扩展的列表视图但是对于父级我必须传递arraylist并且我在getGroupView中获得了类强制转换异常。请帮我写下以下适配器:

PhraseModel.java:

package in.abc.pdfsearchapp;

import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;

public class PhraseModel implements Parcelable {

    private ArrayList<PhraseList> phraseList;

    // constructor

    public PhraseModel(ArrayList<PhraseList> phraseList) {
        this.phraseList = phraseList;
    }

    //---------------------------Getter-Setter--------------------------------------------------------------------------------
    public ArrayList<PhraseList> getPhraseList() {
        return phraseList;
    }

    public void setPhraseList(ArrayList<PhraseList> phraseList) {
        this.phraseList = phraseList;
    }

    // Parcelling

    protected PhraseModel(Parcel in) {
        phraseList=in.createTypedArrayList(PhraseList.CREATOR);
    }

    public static final Creator<PhraseModel> CREATOR = new Creator<PhraseModel>() {
        @Override
        public PhraseModel createFromParcel(Parcel in) {
            return new PhraseModel(in);
        }

        @Override
        public PhraseModel[] newArray(int size) {
            return new PhraseModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(phraseList);
    }


    //-----------------------------------   //PhraseList // ------------------------------------------------------------------------

    public static class PhraseList implements Parcelable {

        public int documentId,noOfOccurences;
        public String documentName,phrase;
        public ArrayList<OccurenceDetails> occurenceDetails;

        //constructor

        public PhraseList(int documentId, String documentName,String phrase, int noOfOccurences,  ArrayList<OccurenceDetails> occurenceDetails) {
            this.documentId = documentId;
            this.documentName = documentName;
            this.phrase = phrase;
            this.noOfOccurences = noOfOccurences;
            this.occurenceDetails = occurenceDetails;
        }
        //------------------------------Getter-Setter--------------------------------------------------------------------------------------

        public int getDocumentId() {
            return documentId;
        }

        public void setDocumentId(int documentId) {
            this.documentId = documentId;
        }

        public int getNoOfOccurences() {
            return noOfOccurences;
        }

        public void setNoOfOccurences(int noOfOccurences) {
            this.noOfOccurences = noOfOccurences;
        }

        public String getDocumentName() {
            return documentName;
        }

        public void setDocumentName(String documentName) {
            this.documentName = documentName;
        }

        public String getPhrase() {
            return phrase;
        }

        public void setPhrase(String phrase) {
            this.phrase = phrase;
        }

        public ArrayList<OccurenceDetails> getOccurenceDetails() {
            return occurenceDetails;
        }

        public void setOccurenceDetails(ArrayList<OccurenceDetails> occurenceDetails) {
            this.occurenceDetails = occurenceDetails;
        }

        //Parcelling

        protected PhraseList(Parcel in) {
            documentId = in.readInt();
            noOfOccurences = in.readInt();
            documentName = in.readString();
            phrase = in.readString();
        }

        public static final Creator<PhraseList> CREATOR = new Creator<PhraseList>() {
            @Override
            public PhraseList createFromParcel(Parcel in) {
                return new PhraseList(in);
            }

            @Override
            public PhraseList[] newArray(int size) {
                return new PhraseList[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(documentId);
            dest.writeString(documentName);
            dest.writeInt(noOfOccurences);
            dest.writeString(phrase);
        }
    }


//---------------------------------    //OccurrenceDetails // --------------------------------------------------------------------

    public static class OccurenceDetails implements Parcelable {

        int occurenceId,pageNo;
        String fullPhrase;

        //constructor

        public OccurenceDetails(int occurenceId, int pageNo, String fullPhrase) {
            this.occurenceId = occurenceId;
            this.pageNo = pageNo;
            this.fullPhrase = fullPhrase;
        }
//-------------------------------Getter-Setter----------------------------------------------------------------------------------

        public int getOccurenceId() {
            return occurenceId;
        }

        public void setOccurenceId(int occurenceId) {
            this.occurenceId = occurenceId;
        }

        public int getPageNo() {
            return pageNo;
        }

        public void setPageNo(int pageNo) {
            this.pageNo = pageNo;
        }

        public String getFullPhrase() {
            return fullPhrase;
        }

        public void setFullPhrase(String fullPhrase) {
            this.fullPhrase = fullPhrase;
        }


        //Parcelling

        protected OccurenceDetails(Parcel in) {
            occurenceId = in.readInt();
            pageNo = in.readInt();
            fullPhrase = in.readString();
        }

        public static final Creator<OccurenceDetails> CREATOR = new Creator<OccurenceDetails>() {
            @Override
            public OccurenceDetails createFromParcel(Parcel in) {
                return new OccurenceDetails(in);
            }

            @Override
            public OccurenceDetails[] newArray(int size) {
                return new OccurenceDetails[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(occurenceId);
            dest.writeInt(pageNo);
            dest.writeString(fullPhrase);
        }
    }
}

ExpandableListAdapter.java:

package in.abc.pdfsearchapp;

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

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

/**
 * Created by Android on 22-Nov-17.
 */

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    Context context;
    public ArrayList<PhraseModel.PhraseList> mPhraseList;
    private HashMap<Integer,ArrayList<PhraseModel.OccurenceDetails>> occurenceList;

    public ExpandableListAdapter(Context context, ArrayList<PhraseModel.PhraseList> mPhraseList, HashMap<Integer, ArrayList<PhraseModel.OccurenceDetails>> occurenceList) {
        this.context = context;
        this.mPhraseList = mPhraseList;
        this.occurenceList = occurenceList;
    }

    public Object getChild(int groupPosition, int childPosition){
        return this.occurenceList.get(this.mPhraseList.get(groupPosition)).get(childPosition);
    }


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


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

        final ArrayList<PhraseModel.OccurenceDetails> childList = (ArrayList<PhraseModel.OccurenceDetails>) getChild(groupPosition, childPosition);
        if(convertView==null){
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView occurenceId,fullPhrase,pageNo;
        occurenceId=(TextView)convertView.findViewById(R.id.occurenceId);
        fullPhrase=(TextView)convertView.findViewById(R.id.fullPhrase);
        pageNo=(TextView)convertView.findViewById(R.id.pageNo);

        occurenceId.setText(childList.get(groupPosition).getOccurenceId());
        fullPhrase.setText(childList.get(groupPosition).getFullPhrase());
        pageNo.setText(childList.get(groupPosition).getPageNo());

        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return this.occurenceList.get(this.mPhraseList.get(groupPosition))
                .size();
    }

    public Object getGroup(int groupPosition) {
        return this.mPhraseList.get(groupPosition);
    }

    public int getGroupCount() {
        return this.mPhraseList.size();
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        ArrayList<PhraseModel.PhraseList> parentList = (ArrayList<PhraseModel.PhraseList>) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView docName,totalOccurences;
        docName=(TextView)convertView.findViewById(R.id.docName);
        totalOccurences=(TextView)convertView.findViewById(R.id.totalOccurences);
        docName.setText(parentList.get(groupPosition).getDocumentName());
        totalOccurences.setText(parentList.get(groupPosition).getNoOfOccurences());
        return convertView;
    }

    public boolean hasStableIds() {
        return false;
    }

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

LandingActivity.java:

    package in.abc.pdfsearchapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class LandingActivity extends AppCompatActivity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    ArrayList<PhraseModel.PhraseList> phraseLists;
    HashMap<Integer, ArrayList<PhraseModel.OccurenceDetails>> occurenceList;

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

        expListView = (ExpandableListView) findViewById(R.id.phraseList);

        // preparing list data
        phraseLists=new ArrayList<PhraseModel.PhraseList>();
        occurenceList=new HashMap<Integer,ArrayList<PhraseModel.OccurenceDetails>>();
        phraseLists.addAll(getmphraseset());

        for (PhraseModel.PhraseList pl: phraseLists){
            ArrayList<PhraseModel.OccurenceDetails>occurenceDetailses=pl.getOccurenceDetails();
            occurenceList.put(pl.getDocumentId(),occurenceDetailses);
        }

        listAdapter = new ExpandableListAdapter(this, phraseLists,occurenceList);

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

        // Listview Group click listener
        expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        phraseLists.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        phraseLists.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();

            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        phraseLists.get(groupPosition)
                                + " : "
                                + occurenceList.get(
                                phraseLists.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });

    }

    private ArrayList<PhraseModel.PhraseList> getmphraseset() {
        try{
            ArrayList<PhraseModel.PhraseList>phrase_Lists = new ArrayList<PhraseModel.PhraseList>();
            ArrayList<PhraseModel.OccurenceDetails>occurenceDetailsArrayList=new ArrayList<PhraseModel.OccurenceDetails>();
            JSONObject jsonObject = new JSONObject(readJSONFromAsset());

            JSONArray phraseArray = jsonObject.getJSONArray("phraseList");
            Log.d("getmphraseset", "phrase count: "+phraseArray.length());
            for (int i = 0; i < phraseArray.length(); i++)
            {
                JSONObject job = phraseArray.getJSONObject(i);

                int documentId =job.getInt("documentId");
                int noOfOccurences=job.getInt("noOfOccurences");

                String documentName=job.getString("documentName");
                String phrase=job.getString("phrase");

                //This i for Occurences array
                ArrayList<PhraseModel.OccurenceDetails> occurencesList = new ArrayList<>();
                JSONArray occurencesArray = job.getJSONArray("occurenceDetails");
                for (int j = 0; j < occurencesArray.length(); j++) {
                    JSONObject jobIn = occurencesArray.getJSONObject(j);

                    int occurenceId=jobIn.getInt("occurenceId");
                    int pageNo=jobIn.getInt("pageNo");
                    String fullPhrase=jobIn.getString("fullPhrase");

                    occurencesList.add(new PhraseModel.OccurenceDetails(occurenceId, pageNo, fullPhrase));

                }

                //here your Phrase[] value store in pdfArrayList
                phrase_Lists.add(new PhraseModel.PhraseList(documentId,documentName,phrase,noOfOccurences,occurencesList));

                Log.i("phraseList size = ", ""+phraseArray.length());
            }

            if (phrase_Lists != null)
            {
                Log.i("phraseList size = ", ""+phraseArray.length());
            }
            return phrase_Lists;
        }catch (JSONException e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public String readJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("hipaJson.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;
    }
}

Logcat:

11-23 15:52:06.428 1519-2794/? E/ActivityManager: applyOptionsLocked: Unknown animationType=0
11-23 15:52:06.428 459-2412/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
11-23 15:52:07.059 29210-29210/? E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
11-23 15:52:11.066 459-2412/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
11-23 15:52:11.152 29210-29210/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: in.hkcl.pdfsearchapp, PID: 29210
                                                   java.lang.ClassCastException: in.hkcl.pdfsearchapp.PhraseModel$PhraseList cannot be cast to java.util.ArrayList
                                                       at in.hkcl.pdfsearchapp.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:81)
                                                       at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2367)
                                                       at android.widget.ListView.makeAndAddView(ListView.java:1972)
                                                       at android.widget.ListView.fillDown(ListView.java:704)
                                                       at android.widget.ListView.fillFromTop(ListView.java:765)
                                                       at android.widget.ListView.layoutChildren(ListView.java:1744)
                                                       at android.widget.AbsListView.onLayout(AbsListView.java:2161)
                                                       at android.view.View.layout(View.java:17548)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5614)

请解释和帮助,我做错了什么?这个应用程序有splashscreen&gt; landingActivity包含可扩展列表,我无法显示为应用程序崩溃,在getGroupView方法中给出了类cat异常。

list_group.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:id="@+id/parent_phraseList_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#BDBDBD"
    app:cardElevation="4dp"
    android:padding="8dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:padding="5dp">

    <TextView
        android:id="@+id/docNameLabel"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Doc Name : "
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/docName"
        android:layout_toRightOf="@id/docNameLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/totalOccurencesLabel"
        android:layout_below="@id/docNameLabel"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Total Occurences : "
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/totalOccurences"
        android:layout_toRightOf="@id/totalOccurencesLabel"
        android:layout_below="@id/docNameLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>

    <ImageButton
        android:id="@+id/linkToDirectOpenDoc_btn"
        android:layout_below="@id/totalOccurencesLabel"
        android:layout_centerHorizontal="true"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/link"
        android:elevation="6dp"
        android:layout_marginTop="5dp"/>

    <ImageButton
        android:id="@+id/expandCard_btn"
        android:layout_below="@id/totalOccurencesLabel"
        android:layout_toRightOf="@id/linkToDirectOpenDoc_btn"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/expand"
        android:elevation="6dp"
        android:layout_marginTop="5dp"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

</RelativeLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        android:padding="8dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:background="#FFFFFF">

            <TextView
                android:id="@+id/occurenceId_label"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="Occurence : "
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/occurenceId"
                android:layout_toRightOf="@id/occurenceId_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/fullPhrase_label"
                android:layout_below="@id/occurenceId_label"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="Full Phrase : "
                android:textStyle="bold"
                android:layout_marginTop="5dp"/>
            <TextView
                android:id="@+id/fullPhrase"
                android:layout_below="@id/occurenceId_label"
                android:layout_toRightOf="@id/fullPhrase_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"/>

            <TextView
                android:id="@+id/pageNo_label"
                android:layout_below="@id/fullPhrase_label"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="Full Phrase : "
                android:textStyle="bold"
                android:layout_marginTop="5dp"/>
            <TextView
                android:id="@+id/pageNo"
                android:layout_below="@id/fullPhrase_label"
                android:layout_toRightOf="@id/pageNo_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

尝试替换

ArrayList<PhraseModel.PhraseList> parentList = (ArrayList<PhraseModel.PhraseList>) getGroup(groupPosition);

通过

PhraseList parentList = (PhraseList) getGroup(groupPosition);

答案 1 :(得分:1)

这可能会有所帮助

 //Change this
 ArrayList<PhraseModel.PhraseList> parentList = (ArrayList<PhraseModel.PhraseList>) getGroup(groupPosition);

 //To
 PhraseModel.PhraseList mPhraseListItem = getGroup(groupPosition);

 //AND getGroup return type to PhraseModel.PhraseList
 public PhraseModel.PhraseList getGroup(int groupPosition) {
    return this.mPhraseList.get(groupPosition);
 }

按要求更新

  

我在ExpandableListAdapter类中进行了一些更改。我尽可能简单。只需复制并粘贴此类。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

Context context;

ExpandableListView expandableListView;

private ArrayList<PhraseModel.PhraseList> mPhraseList;

private int lastExpandedPosition = -1;



public ExpandableListAdapter(Context context, ArrayList<PhraseModel.PhraseList> mPhraseList, ExpandableListView expandableListView) {
    this.context = context;
    this.mPhraseList = mPhraseList;
    this.expandableListView = expandableListView;
}

public PhraseModel.OccurenceDetails getChild(int groupPosition, int childPosition){
    return this.mPhraseList.get(groupPosition).getOccurenceDetails().get(childPosition);
}


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


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

    final PhraseModel.OccurenceDetails occurenceDetails =  getChild(groupPosition, childPosition);
    if(convertView==null){
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView occurenceId,fullPhrase,pageNo;
    occurenceId=(TextView)convertView.findViewById(R.id.occurenceId);
    fullPhrase=(TextView)convertView.findViewById(R.id.fullPhrase);
    pageNo=(TextView)convertView.findViewById(R.id.pageNo);

    occurenceId.setText(String.valueOf(occurenceDetails.getOccurenceId()));
    fullPhrase.setText(occurenceDetails.getFullPhrase());
    pageNo.setText(String.valueOf(occurenceDetails.getPageNo()));

    return convertView;
}

public int getChildrenCount(int groupPosition) {
    return this.mPhraseList.get(groupPosition).getOccurenceDetails().size();
}

public PhraseModel.PhraseList getGroup(int groupPosition) {
    return this.mPhraseList.get(groupPosition);
}

public int getGroupCount() {
    return this.mPhraseList.size();
}

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

public View getGroupView(final int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    PhraseModel.PhraseList parentList =  getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView docName,totalOccurences;
    docName=(TextView)convertView.findViewById(R.id.docName);
    totalOccurences=(TextView)convertView.findViewById(R.id.totalOccurences);
    docName.setText(parentList.getDocumentName());
    totalOccurences.setText(String.valueOf(parentList.getNoOfOccurences()));

    ImageButton linkToDirectOpenDoc_btn, expandCard_btn;

    linkToDirectOpenDoc_btn = (ImageButton) convertView.findViewById(R.id.linkToDirectOpenDoc_btn);
    linkToDirectOpenDoc_btn.setFocusable(false);


    expandCard_btn = (ImageButton) convertView.findViewById(R.id.expandCard_btn);
    expandCard_btn.setFocusable(false);

    expandCard_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                if (lastExpandedPosition == groupPosition) {

                    expandableListView.collapseGroup(lastExpandedPosition);

                    lastExpandedPosition = -1;

                } else {

                    expandableListView.collapseGroup(lastExpandedPosition);

                    lastExpandedPosition = groupPosition;

                    expandableListView.expandGroup(lastExpandedPosition);


                }

        }
    });

    return convertView;
}

public boolean hasStableIds() {
    return false;
}

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

然后在你的Activity下面放置代码来设置适配器和数组列表。这仅供您参考,您必须根据需要进行更改,以便从资产中获取此值。

    //FIRST GROUP

    PhraseModel.OccurenceDetails oneOccurenceDetails = new PhraseModel.OccurenceDetails(1, 20, "Occurence Details Pharse One");
    ArrayList<PhraseModel.OccurenceDetails> firstOccurenceDetails = new ArrayList<>();
    firstOccurenceDetails.add(oneOccurenceDetails);
    PhraseModel.PhraseList onePhraseList = new PhraseModel.PhraseList(1,
            "Doc One", "Pharse One", 10, firstOccurenceDetails);

    //SECOND GROUP
    PhraseModel.OccurenceDetails twoOccurenceDetails = new PhraseModel.OccurenceDetails(2, 30, "Occurence Details Pharse Second");
    ArrayList<PhraseModel.OccurenceDetails> secondOccurenceDetails = new ArrayList<>();
    secondOccurenceDetails.add(twoOccurenceDetails);
    PhraseModel.PhraseList twoPhraseList = new PhraseModel.PhraseList(1,
            "Doc Two", "Pharse Two", 10, secondOccurenceDetails);


    ArrayList<PhraseModel.PhraseList> onePhraseArrayList = new ArrayList<>();
    onePhraseArrayList.add(onePhraseList);
    onePhraseArrayList.add(twoPhraseList);

    ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(this, onePhraseArrayList, expandableListView);

    expandableListView.setAdapter(expandableListAdapter);

答案 2 :(得分:0)

WITH tbl(p_xml) AS (
   SELECT '<root>
    <e id="1">
        <p1>P1</p1>
        <p2>P2</p2>
    </e>
    <e id="2">
        <p1>P1</p1>
        <p3>P2</p3>
    </e>
    <e id="3">
        <p2>P1</p2>
        <p3>P3</p3>
    </e>
</root>'::xml
)
SELECT e
FROM tbl
CROSS JOIN LATERAL UNNEST(xpath('//e[p1 and not(p3)]', p_xml)) e