Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何在单击expandablelistview时获取子级的父级名称_Android_Expandablelistview_Expandablelistadapter_Android Expandable List View - Fatal编程技术网

Android如何在单击expandablelistview时获取子级的父级名称

Android如何在单击expandablelistview时获取子级的父级名称,android,expandablelistview,expandablelistadapter,android-expandable-list-view,Android,Expandablelistview,Expandablelistadapter,Android Expandable List View,我创建了一个类似于下面的 下面是我的代码 ThreeLevelListAdapter public class ThreeLevelListAdapter extends BaseExpandableListAdapter { String[] parentHeaders; List<String[]> secondLevel; private Context context; List<LinkedHashMap<String, String[]>> d

我创建了一个类似于下面的

下面是我的代码

ThreeLevelListAdapter

public class ThreeLevelListAdapter extends BaseExpandableListAdapter {

String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;

/**
 * Constructor
 * @param context
 * @param parentHeader
 * @param secondLevel
 * @param data
 */
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
    this.context = context;

    this.parentHeaders = parentHeader;

    this.secondLevel = secondLevel;

    this.data = data;
}


@Override
public int getGroupCount() {
    return parentHeaders.length;
}

@Override
public int getChildrenCount(int groupPosition) {


    // no idea why this code is working

    return 1;

}

@Override
public Object getGroup(int groupPosition) {

    return groupPosition;
}

@Override
public Object getChild(int group, int child) {


    return child;


}



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

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

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



@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_first, null);
    TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
    text.setText(this.parentHeaders[groupPosition]);

    return convertView;
}

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

    final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);

    String[] headers = secondLevel.get(groupPosition);


    List<String[]> childData = new ArrayList<>();
    HashMap<String, String[]> secondLevelData = data.get(groupPosition);

    for (String key : secondLevelData.keySet()) {


        childData.add(secondLevelData.get(key));

    }


    secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));

    secondLevelELV.setGroupIndicator(null);


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

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


    return secondLevelELV;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;


List<String[]> data;

String[] headers;

ImageView ivGroupIndicator;


public SecondLevelAdapter(Context context, String[] headers, List<String[]> data) {
    this.context = context;
    this.data = data;
    this.headers = headers;

}

@Override
public Object getGroup(int groupPosition) {

    return headers[groupPosition];
}

@Override
public int getGroupCount() {

    return headers.length;
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_second, null);
    TextView text = (TextView) convertView.findViewById(R.id.rowSecondText);
    String groupText = getGroup(groupPosition).toString();
    text.setText(groupText);
    return convertView;
}

@Override
public Object getChild(int groupPosition, int childPosition) {

    String[] childData;

    childData = data.get(groupPosition);


    return childData[childPosition];
}

@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) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.row_third, null);

    TextView textView = convertView.findViewById(R.id.rowThirdText);
    textView.setOnClickListener(v -> {
        // here I want to get the all parent names of the child clicked
        Common.showToast(context,"This Feature is under development", Toast.LENGTH_LONG);
    });


    String[] childArray = data.get(groupPosition);

    String text = childArray[childPosition];

    textView.setText(text);

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    String[] children = data.get(groupPosition);


    return children.length;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}
我怎样才能做到这一点


任何帮助都将不胜感激。

步骤1:您已经有了在“回收器”视图中显示的元素列表。现在创建这些列表的列表好的(我稍后会告诉你为什么?)

步骤2:您可以创建另一个全局列表(如果经常使用,也可以是静态的)。提示:如果适配器在其他类中,则通过构造函数传递列表

说明: 此列表将保存单击项的索引(我假设我们不知道recycler视图中有多少子列表)。当用户单击第一个回收器视图列表中的项目时,我们将通过(~adapter.getAdapterPosition)保存索引 ())然后用户单击子列表,然后我们保存索引,依此类推

现在,假设用户按如下方式单击:

  • 从列表1->索引:4
  • 从列表2->索引:2
  • 从列表3->索引:3
现在我们要做的是,我们有一个包含索引的列表和另一个包含n个列表的列表(其中n=子列表的数量)。listsArrayList按顺序包含列表,indexClickedList包含我们单击的元素的索引,indexClickedList中的索引是我们单击该项的列表编号,因此您可以从ListList访问该项。通过这种方式,您可以获得我们单击的元素的父项。这里必须使用while循环,因为我们不知道有多少子列表。继续,直到我们到达list.get(whatEverIndex==null)


每当我们有分支时,我们总是使用树结构,在第一个注释中,我以为您只有一个子列表,但现在在您的例子中,我们有多个子列表。所以你一定要使用树形结构。创建循环时要小心,可能会出现空指针异常。

使用全局变量或inheritance@Vijay我如何使用它们?@Vijay从不使用全局变量找到一种方法来保持单击的层次结构。一个选项是保留树结构,以便在单击一个元素后可以轻松检查节点层次结构。呈现子项时,可以传递父项。所以,每个项目都知道谁是它的父母谢谢你的回答。我使用的是
ScrollView
而不是
RecyclerView
。我还有一个
公共类SecondLevelExpandableListView扩展了ExpandableListView{public SecondLevelExpandableListView(上下文){super(上下文);}受保护的度量空(int-widthMeasureSpec,int-heightMeasureSpec){heightmeaspect=measpect.makemeaspect(9999999,measpect.AT_最多);super.onMeasure(widthmaspect,heightmeaspect);}
我使用它就像
final second SecondLevelExpandableListView secondLevelELV=new SecondLevelExpandableListView(context)
三级列表适配器的
getChildView
标题中
我正在获取孩子的直接父母姓名,例如
M/S PAK MEDICOSE PHARMECY
父母是
LICE-O-NIL CREAM
,父母是
1112 STOP Town
好的,然后就变得很容易了,就像你读我的报告一样oach.Now在每个类中创建方法作为getParentsName(),它应该是递归的,就像第三个应该调用第二个,然后首先停止(使用null或任何东西)。你是说我们得到了直接的父级,然后它就变得非常容易了。getParentsName()应该从父类重写,我们可以很容易地获得父类名称。通过创建树结构的方式,您可以通过调用while(child.getParent()!=null){list.add(child.getName())}来访问父类。类似于此。给你的提示:将来,在应用自定义方式(如ExpandableList、musicPlayer、videoPlayer等)之前,一定要进行研究。有很多库可用于此类任务。这种习惯可以节省大量时间和精力。在google“ExpandableList library android github”上搜索
 TextView textView = convertView.findViewById(R.id.rowThirdText);
    textView.setOnClickListener(v -> {
        //Here I want to get the names of parent
        Common.showToast(context,"This Feature is under development", Toast.LENGTH_LONG);
    });