как войти в действие, основанное на конкретном раскрывающемся списке ListView childPosition & groupPosition (Three Level) Android - PullRequest
0 голосов
/ 26 декабря 2018

Как получить элемент третьего уровня из расширяемого элемента ListView, мне нужно перейти к конкретному действию на основе элемента listview.Помогите мне, ребята.

 public class MainActivity extends AppCompatActivity {

private ExpandableListView expandableListView;

String[] parent = new String[]{"Rooms"};
String[] q1 = new String[]{"Living Room", "Dining Room","Bed Room","Kitchen","Outdoor Living"};
String[] q2 = new String[]{"Linear Layout", "Relative Layout"};
String[] q3 = new String[]{"Recycle View"};
String[] des1 = new String[]{"Living Room Sets","Sofas & LoveSeats","Sectional Sofas","Sofa Sleeper","Pillows","Chairs","Coffee & End tables","Table Sets","Consoles & Desks","TV Consoles","Ottoman","Accent tables","Chaises","Pouf","Recliners","Serving Carts","Stools"};


LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
 * Second level array list
 */
List<String[]> secondLevel = new ArrayList<>();
/**
 * Inner level data
 */
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();


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

private void setUpAdapter() {
    secondLevel.add(q1);
    secondLevel.add(q2);
    secondLevel.add(q3);
    thirdLevelq1.put(q1[0], des1);
    thirdLevelq1.put(q1[1], des1);
    thirdLevelq1.put(q1[2], des1);
    thirdLevelq1.put(q1[3], des1);
    thirdLevelq1.put(q1[4], des1);


    data.add(thirdLevelq1);
    data.add(thirdLevelq2);
    data.add(thirdLevelq3);
    expandableListView = (ExpandableListView) findViewById(R.id.expandible_listview);
    //passing three level of information to constructor
    ThreeLevelListAdapter threeLevelListAdapterAdapter = new ThreeLevelListAdapter(this, parent, secondLevel, data);
    expandableListView.setAdapter(threeLevelListAdapterAdapter);
  expandableListView.setOnChildClickListener(new 
 ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {

  //here I got confused
 // I need to get a particular child item for the String data variable
        String info= thirdLevelq1.get(groupPosition).get(childPosition);
        if(info=="chairs")
        {
            Intent i1 = new Intent(getApplicationContext(), chairs.class);
            startActivity(i1);
        }
            return false;
        }
    });
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousGroup = -1;

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


}

}

ниже представлен трехуровневый адаптер второго уровня, расширяемый listView

 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 = (TextView) convertView.findViewById(R.id.rowThirdText);
    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;
}

}

ниже - третий уровеньАдаптер трехуровневого расширяемого списка ListView

 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;
}

}

ниже - SecondLevelExpandableListView трехуровневого расширяемого списка ViewView

 public class SecondLevelExpandableListView extends ExpandableListView {

public SecondLevelExpandableListView(Context context) {
    super(context);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

...