Я надеюсь, что GridView внутри ExpandableLisView вот так.
Это мой код:
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 с ImageView