Android中ExpandableLisView内的GridView

时间:2018-11-26 06:41:54

标签: android gridview imageview expandablelistview

我希望GridView在ExpandableLisView like this.

这是我的代码:

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

我不知道为什么item_icon.setImageResource会出错?

我想念什么?

更新:

MyGridViewAdapter.java

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

但是我得到了NullPointerException

我尝试在MainActivity中显示图像视图,这是我的数据:

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

更新:

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

我希望这样: GridView with ImageView

3 个答案:

答案 0 :(得分:1)

item_icon是一个ImageView对象,它的整数值位于R.java文件中。

我想为integer中显示的图片获取ImageView资源是错误的。尝试以下代码。

item_icon.setImageResource(--put here some actual resources--); 

答案 1 :(得分: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);
}

结果是:ExpandableListView child with imageView and TextView

但是我仍然发现孩子是gridView或gridLayout...。

答案 2 :(得分:-1)

item_icon不是列表,对吧?那么如何从item_icon获取职位?

你有

c.execute("""INSERT INTO tb_transaction VALUES(:empID, :date, :time_in_morning, :time_out_morning, :time_in_afternoon, :time_out_afternoon,:Tardy,:Undertime)""",
                  {--here i want to insert like just the empID, time_in_morning and time_out_morning--})

作为要传递给item_name的列表

类似地,您应该具有要显示的图像的列表