想实现按日期分类照片,功能大概已经实现
有3个组 "2012-04-05", "2012-04-06", "2012-04-12"
Run程序后我单击 2012-04-05这个组可以显示对应的照片,但当我继续单击2012-04-06的时候出现的是
2012-04-05这个组下面的照片,接着2012-04-12的也是一样的情况
我重新Run,单击2012-04-12,出现的照片稳和,单击其他两个组,悲剧重演,请高手指教。。。
代码如下
ListViewActivity
- Java code
public class ListViewActivity extends Activity{ ExpandableListView expandableListView; ListViewAdapter treeViewAdapter; public String[] groups = { "2012-04-05", "2012-04-06", "2012-04-12"}; public String[][] child = { { "" }, { "" }, { "" } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); treeViewAdapter = new ListViewAdapter(this, ListViewAdapter.PaddingLeft >> 1); expandableListView = (ExpandableListView) this .findViewById(R.id.expandableListView); List<ListViewAdapter.TreeNode> treeNode = treeViewAdapter.GetTreeNode(); for (int i = 0; i < groups.length; i++) { ListViewAdapter.TreeNode node = new ListViewAdapter.TreeNode(); node.parent = groups[i]; for (int ii = 0; ii < child[i].length; ii++) { node.childs.add(child[i][ii]); } treeNode.add(node); } treeViewAdapter.UpdateTreeNode(treeNode); expandableListView.setAdapter(treeViewAdapter); } class MyGridView extends GridView { public MyGridView(android.content.Context context, android.util.AttributeSet attrs) { super(context, attrs); } /** * 设置不滚动 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }}
- Java code
public class ListViewAdapter extends BaseExpandableListAdapter implements OnItemClickListener { public static final int ItemHeight = 48;// 每项的高度 public static final int PaddingLeft = 36;// 每项的高度 private int myPaddingLeft = 0; List<String> childlist; private MyGridView toolbarGrid; private List<List<String>> mtimage_array = new ArrayList<List<String>>(); private List<TreeNode> treeNodes = new ArrayList<TreeNode>(); private Context parentContext; private LayoutInflater layoutInflater; static public class TreeNode { Object parent; List<Object> childs = new ArrayList<Object>(); } public ListViewAdapter(Context view, int myPaddingLeft) { parentContext = view; this.myPaddingLeft = myPaddingLeft; } public List<TreeNode> GetTreeNode() { return treeNodes; } public void UpdateTreeNode(List<TreeNode> nodes) { treeNodes = nodes; } public void RemoveAll() { treeNodes.clear(); } public Object getChild(int groupPosition, int childPosition) { return treeNodes.get(groupPosition).childs.get(childPosition); } public int getChildrenCount(int groupPosition) { return treeNodes.get(groupPosition).childs.size(); } static public TextView getTextView(Context context) { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ItemHeight); TextView textView = new TextView(context); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); return textView; } /** * 可自定义ExpandableListView */ public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { getPictures("/mnt/sdcard/photoimages"); if (convertView == null) { layoutInflater = (LayoutInflater) parentContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.view, null); toolbarGrid = (MyGridView) convertView .findViewById(R.id.GridView_toolbar); toolbarGrid.setNumColumns(4);// 设置每行列数 toolbarGrid.setGravity(Gravity.CENTER);// 位置居中 toolbarGrid.setHorizontalSpacing(10);// 水平间隔 toolbarGrid.setAdapter(getMenuAdapter(getChildpicture(groupPosition)));// 设置菜单Adapter toolbarGrid.setOnItemClickListener(this); } return convertView; } /** * 可自定义list */ public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getTextView(this.parentContext); textView.setText(getGroup(groupPosition).toString()); textView.setPadding(myPaddingLeft + PaddingLeft, 0, 0, 0); return textView; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public Object getGroup(int groupPosition) { return treeNodes.get(groupPosition).parent; } public int getGroupCount() { return treeNodes.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } /** * 构造菜单Adapter * * @param menuNameArray * 名称 * @param mtimage_array2 * 图片 * @return SimpleAdapter */ private SimpleAdapter getMenuAdapter(List<String> mtimage_array2) { ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < mtimage_array2.size(); i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("itemImage", mtimage_array2.get(i)); data.add(map); } SimpleAdapter simperAdapter = new SimpleAdapter(parentContext, data, R.layout.item_menu, new String[] { "itemImage" }, new int[] { R.id.item_image }); childlist.clear(); return simperAdapter; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(parentContext, "当前选中的是:" + position, Toast.LENGTH_SHORT) .show(); } /*public void ishavedatatime( List <String> listdata) { for(int i=0;i<mtimage_array.size();i++) { if(mtimage_array.get(i).get(0)==listdata.get(0)) { mtimage_array.get(i).add(listdata.get(1)); break; } } mtimage_array.add(listdata); }*/ public void getPictures(String itemcs) { File file = new File(itemcs); File[] files = file.listFiles(); for(int i = 0; i < files.length; i++) { final File f = files[i]; if(f.isFile()) { try{ int idx = f.getPath().lastIndexOf("."); if (idx <= 0) { continue; } String suffix = f.getPath().substring(idx); if (suffix.toLowerCase().equals(".jpg") || suffix.toLowerCase().equals(".jpeg") || suffix.toLowerCase().equals(".bmp") || suffix.toLowerCase().equals(".png") || suffix.toLowerCase().equals(".gif") ) { List <String> lists =new ArrayList<String>(); String datatime=f.getName().substring(0,10); String path=f.getPath(); lists.add(datatime); lists.add(path); mtimage_array.add(lists); lists=null; } } catch(Exception e) { e.printStackTrace(); } } } } public List<String> getChildpicture(int grouppos) { ListViewActivity li=new ListViewActivity(); childlist=new ArrayList<String>(); String jj=li.groups[grouppos]; for(int i=0;i<mtimage_array.size();i++) { String cc=mtimage_array.get(i).get(0).toString(); if(jj.equals(cc)) { childlist.add(mtimage_array.get(i).get(1)); } } return childlist; } }
------解决方案--------------------
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
getPictures("/mnt/sdcard/photoimages");
if (convertView == null) {
layoutInflater = (LayoutInflater) parentContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.view, null);
toolbarGrid = (MyGridView) convertView
.findViewById(R.id.GridView_toolbar);
toolbarGrid.setNumColumns(4);// 设置每行列数
toolbarGrid.setGravity(Gravity.CENTER);// 位置居中
toolbarGrid.setHorizontalSpacing(10);// 水平间隔
toolbarGrid.setAdapter(getMenuAdapter(getChildpicture(groupPosition)));// 设置菜单Adapter
toolbarGrid.setOnItemClickListener(this);
}
return convertView;
}
——————————————
convertView第一次执行之后就不为NULL了,你把if去掉,就可以了。这是一种缓存机制。或者你把if里的一句话提出来放在if语句后面。
toolbarGrid.setAdapter(getMenuAdapter(getChildpicture(groupPosition)));
这样每次执行该方法的时候,就会设置一次适配器。
------解决方案--------------------
从这个方法上看。你的convertView应该是个全局变量。为什么执行一次 if (convertView == null) {
你这句话。就导致了只执行一次这个问题。