- package com.cloudxp;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.GridView;
- import android.widget.ListAdapter;
- import android.widget.SimpleAdapter;
- public class EditCourseIcon extends Activity {
- private GridView mygridview;
- private List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.editcourseicon);
- //准备要添加的数据条目
- for (int i = 0; i < 10; i++) {
- Map<String, Object> item = new HashMap<String, Object>();
- item.put("imageItem", R.drawable.icon);
- item.put("textItem", "text" + i);
- items.add(item);
- }
- //实例化一个适配器
- SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.editcourseiconitem, new String[]{"imageItem", "textItem"}, new int[]{R.id.image_item, R.id.text_item});
- //获得GridView实例
- mygridview = (GridView) findViewById(R.id.mygridview);
- //gridview.setNumColumns(3);//可以在xml中设置
- //gridview.setGravity(Gravity.CENTER);//同上
- //将GridView和数据适配器关联
- mygridview.setAdapter(adapter);
- //设置监听器
- mygridview.setOnItemClickListener(new ItemClickListener());
- }
- class ItemClickListener implements OnItemClickListener
- {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- // TODO Auto-generated method stub
- System.out.println(position);
- System.out.println(id);
- parent.getItemAtPosition(position);
- System.out.println(parent.getItemAtPosition(position));
- /*public void UpdateList(int selectedItem)
- {
- ListAdapter la = m_List.getAdapter();
- int itemNum = m_List.getCount();
- for(int i=0; i<itemNum; i++)
- {
- HashMap<String, Object> map = (HashMap<String, Object>)la.getItem(i);
- if ( i == selectedItem)
- map.put("ItemIcon", R.drawable.image1);
- else
- map.put("ItemIcon", R.drawable.image2);
- }
- ((SimpleAdapter)la).notifyDataSetChanged();
- }*/
- if(position==parent.getCount()-1)
- {
- ListAdapter la = mygridview.getAdapter();
- HashMap<String, Object> item = new HashMap<String, Object>();
- item.put("imageItem", R.drawable.yw);
- int i=position+1;
- item.put("textItem", "text" + i);
- items.add(item);
- ((SimpleAdapter)la).notifyDataSetChanged();
- }
- Object lastObject = parent.getItemAtPosition(position+1);
- Object object = parent.getItemAtPosition(position);
- items.set(position, (Map<String, Object>)lastObject);
- items.set(position+1, (Map<String, Object>)object);
- }
- }
- }
GridView动态增加图片
最新推荐文章于 2020-11-16 17:30:59 发布