1

I hope GridView inside ExpandableLisView like this.

This is my code :

MainActivity.java:

private List<List<String>> itemList; //child's data

ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(MainActivity.this, groupList, itemList);

ExpandableListViewAdapter.java:

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

    gridView = (GridView) convertView;
    MyGridViewAdapter gridViewAdapter = new MyGridViewAdapter(context, itemList.get(groupPosition));
    gridView.setAdapter(gridViewAdapter);

MyGridViewAdapter.java

List<String> itemGridList;

public View getView(int position, View convertView, ViewGroup parent) {

    if (null == convertView) {
            convertView = View.inflate(context, R.layout.gridview_item, null);
        }
    TextView item_name = (TextView) convertView.findViewById(R.id.item_name);
    ImageView item_icon = (ImageView) convertView.findViewById(R.id.item_icon);

    item_name.setText(itemGridList.get(position));
    item_icon.setImageResource(item_icon.get(position)); //get error?
}

I don't know why item_icon.setImageResource will get error ?

what I am missing?

Update :

MyGridViewAdapter.java

int[] imageId; //add
item_icon.setImageResource(imageId[position]);

but I get NullPointerException

I try to show imageviews in MainActivity , this is my data :

private List<List<String>> itemList;

List<String> groupList = initData();
ExpandableListViewAdapter adapter = new ExpandableListViewAdapter(MainActivity.this, groupList, itemList);
elv.setAdapter(adapter);

   private List<String> initData() {
     List<String> groupList = new ArrayList<>();
     List<String> itemGridList1 = new ArrayList<>();
     String[] item1 = getResources().getStringArray(R.array.sport);
     itemGridList1.add(item1[0]);
     itemGridList1.add(item1[1]);

     List<String> itemGridList2 = new ArrayList<>();
     String[] item2 = getResources().getStringArray(R.array.healthy);
     itemGridList2.add(item2[0]);
     itemGridList2.add(item2[1]);

     itemList = new ArrayList<>();
     itemList.add(itemGridList1);
     itemList.add(itemGridList2);
     return groupList;
    }

Update :

ExpandableListActivity.java

private int[] logos = new int[] { R.drawable.fitness, R.drawable.walking, R.drawable.temp };
private String[] generalsTypes = new String[] { "Sport", "Healthy", "Measure" };
private String[][] generals = new String[][] {
        {"Swimming", "Hiking", "Climbing", "Biking"},
        {"Walking" , "Running"},
        {"Measure"}
};
private int[][] generallogos = new int[][]{
        { R.drawable.ic_swimming, R.drawable.ic_hiking, R.drawable.ic_climbing, R.drawable.ic_bike },
        { R.drawable.ic_walk, R.drawable.ic_running },
        { R.drawable.ic_matters }
};

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ExpandableListView elv = findViewById(R.id.expenview);
    elv.setAdapter(adapter);
    elv.setGroupIndicator(null);
}

ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
   @Override
    public int getGroupCount() {
        return generalsTypes.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return generals[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return generalsTypes[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return generals[groupPosition][childPosition];
    }

    @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) {
        LinearLayout ll = new LinearLayout(
                ExpandableListActivity.this);
        ImageView logo = new ImageView(ExpandableListActivity.this);
        logo.setImageResource(logos[groupPosition]);
        logo.setPadding(50, 0, 0, 0);
        ll.addView(logo);
        TextView textView = getTextView();
        textView.setTextColor(Color.BLACK);
        textView.setText(getGroup(groupPosition).toString());
        ll.addView(textView);
        return ll;
    }

     @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LinearLayout ll = new LinearLayout(
                ExpandableListActivity.this);
        ImageView generallogo = new ImageView(
                ExpandableListActivity.this);
        generallogo
                .setImageResource(generallogos[groupPosition][childPosition]);
        ll.addView(generallogo);
        TextView textView = getTextView();
        textView.setText(getChild(groupPosition, childPosition)
                .toString());
        ll.addView(textView);
        return ll;
    }

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

I hope like this : GridView with ImageView

Tzuyu Lin
  • 47
  • 7
  • List list1 = new ArrayList(); list1.add(R.drawable.tick); holder.img_status.setImageResource(list1.get(position)); – Abraham Mathew Nov 26 '18 at 09:00
  • Or try it like this ---> int[] l; l = new int[]{R.drawable.tick,R.drawable.tick}; holder.img_status.setImageResource(l[position]); – Abraham Mathew Nov 26 '18 at 09:11
  • @ABr , Can I just modify MainActivity only ? itemGridList1.add("swimming", R.deawable.pic01); it seems more easy? – Tzuyu Lin Nov 26 '18 at 09:48
  • Whats this groupList and itemList inside MainActivity? Can you please explain the use of private List> itemList? Do you reallly require a List? – Abraham Mathew Nov 26 '18 at 10:05
  • Actually what you need is a String and a corresponding Drawable am i Right? – Abraham Mathew Nov 26 '18 at 10:20
  • @ABr , Yes , I need is a String and a corresponding Drawable. – Tzuyu Lin Nov 27 '18 at 01:50
  • Ok Then you can make a custom item list i.e., List itemlist = new Arraylist() where Tact is a new model with a String field and a drawable field – Abraham Mathew Nov 27 '18 at 04:07
  • @Abr , Any sample code ? – Tzuyu Lin Nov 27 '18 at 04:09
  • from where are you getting this itemGridList? Is it hardcoded or is it from the api? – Abraham Mathew Nov 27 '18 at 04:15
  • data class Tact( val activity: String, val drawable: Int ) – Abraham Mathew Nov 27 '18 at 04:28
  • @ABr, List itemGridList – Tzuyu Lin Nov 27 '18 at 05:08
  • No i mean the values in the itemGridList!! are you hard coding it in the app – Abraham Mathew Nov 27 '18 at 05:30
  • I think what you need is a single child view with **GridLayout**, not GridView. This link may help: https://stackoverflow.com/questions/49914836/checkbox-for-gridview-inside-listview-in-android/49934675#49934675 If you really need a GridView, this link may help: https://stackoverflow.com/questions/51868333/gridview-within-expandablelistview-with-database/51887911#51887911 You may even use ViewPager, this link: https://stackoverflow.com/questions/50288713/expandablelistview-with-viewpager-combination-as-its-child/50383160#50383160 – i_A_mok Nov 28 '18 at 03:22

3 Answers3

1

item_icon is an ImageView Object and it is given integer value which is located in R.java file.

I thing you are doing mistake to get integer resource for Image to be shown in ImageView Try with below code.

item_icon.setImageResource(--put here some actual resources--); 
Chetan Joshi
  • 5,279
  • 4
  • 24
  • 33
0
   private List<String> Group;
   private List<List<String>> Child;
   private List<String> Child_1;
   private List<String> Child_2;
   private List<String> Child_3;
   private List<List<Integer>> ChildPicture;

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

    Group = new ArrayList<String>();
    Group.add("SPORT");
    Group.add("Healthy");
    Group.add("Measure");

    Child_1 = new ArrayList<String>();
    Child_1.add("Swimming");
    Child_1.add("Biking");
    Child_1.add("Hiking");

    Child_2 = new ArrayList<String>();
    Child_2.add("Walking");
    Child_2.add("Running");

    Child_3 =new ArrayList<String>();
    Child_3.add("Measure");

    Child = new ArrayList<List<String>>();
    Child.add(Child_1);
    Child.add(Child_2);
    Child.add(Child_3);

    List<Integer> ChildPic_1 = new ArrayList<Integer>();
    ChildPic_1.add(R.drawable.ic_swimming);
    ChildPic_1.add(R.drawable.ic_bike);
    ChildPic_1.add(R.drawable.ic_hiking);

    List<Integer> ChildPic_2 = new ArrayList<Integer>();
    ChildPic_2.add(R.drawable.ic_walk);
    ChildPic_2.add(R.drawable.ic_running);

    List<Integer> ChildPic_3 = new ArrayList<Integer>();
    ChildPic_3.add(R.drawable.ic_matters);

    ChildPicture = new ArrayList<List<Integer>>();
    ChildPicture.add(ChildPic_1);
    ChildPicture.add(ChildPic_2);
    ChildPicture.add(ChildPic_3);

    ExpandableListView elv = findViewById(R.id.expenview);
    elv.setAdapter(new MyExpandableAdapter(Main2Activity.this));
    elv.setGroupIndicator(null);
}

the reasult is :ExpandableListView child with imageView and TextView

but I still find out that child is gridView or gridLayout ....

Tzuyu Lin
  • 47
  • 7
-1

item_icon is not a list Right? So how can you get the position from item_icon?

you have

List<String> itemGridList;

as the list that you are passing to the item_name

Similarly you should have a list for the images to be shown

Abraham Mathew
  • 1,255
  • 1
  • 9
  • 32