ListView is nested within GridView in android, and the content of one GridView cannot be displayed?

  android, question

describe

I nested a GridView in ListView, that is, there is a GridView in each item.
A group of pictures is displayed in GridView.

Code

Adapter for ListView

The following is
GetView in ListView’s adapter.
In the list is the Uri of a group of pictures.
I made a judgment. If list inside has no Uri, GridView; will be hidden. If you have talent, set the adapter for GridView to display.

@Override
 public View getView(int arg0, View convertView, ViewGroup arg2) {
 
 ViewHolder viewHolder;
 if (convertView == null) {
 convertView = inflater.inflate(R.layout.listview_main_item, null);
 viewHolder = new ViewHolder();
 
 viewHolder.mImgGridView = (GridView) convertView
 .findViewById(R.id.gv_listView_main_gridView);
 convertView.setTag(viewHolder);
 } else {
 viewHolder = (ViewHolder) convertView.getTag();
 bracket
 ArrayList<String> list = (ArrayList<String>) initData().get(arg0).get("urls");
 if (list.size() == 0) {
 viewHolder.mImgGridView.setVisibility(View.GONE);
 } else {
 Log.d("PromotionalMaterialActi ",""plus list.size ());
 viewHolder.mImgGridView.setAdapter(new MyGridViewAdapter(context, list));
 bracket
 return convertView;
 bracket

Data

ArrayList<Map<String, Object>> mDatas = new ArrayList<Map<String, Object>>();
 
 //No pictures
 Map<String, Object> map = new HashMap<>();
 ArrayList<String> urls = new ArrayList<String>();
 map.put("urls", urls);
 mDatas.add(map);
 
 //A picture
 Map<String, Object> map2 = new HashMap<>();
 ArrayList<String> urls2 = new ArrayList<String>();
 urls2.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
 map2.put("urls", urls2);
 mDatas.add(map2);
 
 //Three pictures
 Map<String, Object> map3 = new HashMap<>();
 ArrayList<String> urls3 = new ArrayList<String>();
 urls3.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
 urls3.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
 urls3.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
 map3.put("urls", urls3);
 mDatas.add(map3);
 
 //six pictures
 Map<String, Object> map4 = new HashMap<>();
 ArrayList<String> urls4 = new ArrayList<String>();
 urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698837_7507.jpg");
 urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698865_3560.jpg");
 urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
 urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
 urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
 urls4.add("http://img.my.csdn.net/uploads/201410/19/1413698839_2302.jpg");
 map4.put("urls", urls4);
 mDatas.add(map4);
 
 //six pictures
 Map<String, Object> map5 = new HashMap<>();
 ArrayList<String> urls5 = new ArrayList<String>();
 urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698837_7507.jpg");
 urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698865_3560.jpg");
 urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698867_8323.jpg");
 urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698837_5654.jpg");
 urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698883_5877.jpg");
 urls5.add("http://img.my.csdn.net/uploads/201410/19/1413698839_2302.jpg");
 map.put("urls", urls5);
 mDatas.add(map5);

Description of phenomena

The GridView picture on the fourth item cannot be displayed
图片描述
图片描述
图片描述

Solution

TheviewHolder.mImgGridView.setVisibility(View.GONE);
In this way, the GridView picture on the fourth item will be displayed.
But Why is this?

if (list.size() == 0) {
 viewHolder.mImgGridView.setVisibility(View.GONE);
 } else {
 Log.d("PromotionalMaterialActi ",""plus list.size ());
 viewHolder.mImgGridView.setAdapter(new MyGridViewAdapter(context, list));
 bracket

Modified into

if (list.size() == 0) {
 viewHolder.mImgGridView.setVisibility(View.GONE);
 } else {
 Log.d("PromotionalMaterialActi ",""plus list.size ());
 viewHolder.mImgGridView.setVisibility(View.VISIBLE);
 viewHolder.mImgGridView.setAdapter(new MyGridViewAdapter(context, list));
 bracket

The reason for this modification is: in some Item, list is empty, so you will set the mImgGridView property to GONE. However, when ListView retrieves this View reuse from Holder, this mImgGridView is always GONE, so you need to manually set it to VISIBLE.