• android ListView 与GridView 学习总结(五)


    ListView的使用总结
     基本使用:
       步骤:在布局文件中定义一个ListView控件-在活动中获得ListView的实例-获得适配器adapter的实例并且传入三个参数-把适配器对象传递给listview实例

    public class MainActivity extends Activity{
    	ListView contactsView;
    	ArrayAdapter<String> adapter;
    	List<String> contactsList=new ArrayList<String>();
    	protected void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		contactsView=(ListView)findViewById(R.id.contactsView);
    		adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
    		contactsView.setAdapter(adapter);
    		readContacts();
    	}
    	public void readContacts(){
    		Cursor cursor=null;
    		try{
    		 cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
    		 while(cursor.moveToNext()){
    			String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    			String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    			contactsList.add(name+"/n"+number);
    		 }
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			if(cursor!=null){
    			cursor.close();
    			}
    		}
    	}
    }  
    

     自定义的ListView的使用
     步骤:自定义适配器适配类型-为每一个子项制定自定义布局(item——layout)-创建自定义的适配器,将每一个子项的内容指定给布局控件-
     -获得listview 的实例以及一个容纳所有子项的list实例和适配器实例-把适配器传入布局
    例如一个水果自定义listview

    public class Fruit(){
        private String name;
        private int ImageId;
        public Fruit(String name,int ImageId){
            this.name=name;
            this.ImageId=ImageId;
        }
        public String getName(){
            return name;
        }
        public int ImageId(){
            return ImageId;
        }
    }
    fruit_item.xml
    ...
    <ImageView
    .../>
    <TextView
    .../>
    ...
    public class FruitAdapter extends ArrrayAdapter(Fruit){
        private int resourceId;
        public FruitAdapter(Context context,int textViewResourceId,List<Fruit>objects){
            super(context,textViewResourceId,objects);
            resourceId=textViewResourceId;
        }
        public View getView(int position,View convertView,ViewGroup parent){
            Fruit fruit=getItem(position);
            View view=LayoutInflater.from(getContext()).inflate(resourceId,null);
            ImageView fruitImage=(ImageView) view.findViewById(R.id.fruit_image);
            TextView fruitName=(TextView) view.findViewById(R.id.fruit_name);
            fruitImage.setImageResource(fruit.getImageId());
            fruiName.setText(fruit.getName());
            return view;
        }
    
    }
    mainactivity
    ....

    gridview的使用
    几个属性:
    android:numColumns="auto_fit" --------列数设置为自动
    android:columnWidth="90dp",----------每列的宽度,也就是Item的宽度
    android:stretchMode="columnWidth"------缩放与列宽大小同步
    android:verticalSpacing="10dp"----------垂直边距
    android:horizontalSpacing="10dp"-------水平边距

    public void mainactivity extends Activity{
        private GridView gv;
        private List<Map<String,Objects>> data_list=new ArrayList<Map<String,Objects>>();
        private SimpleAdapter<map<String,Objects>> adapter;
        private int[] image={R.id...,.....,......};
        private String[] name={.....};
        public void onCreate(Bundle savedInstanceState){
            super.(savedInstanceState);
            setContentView(R.layout.test);
            gv=(GridView)findViewById(R.id.gridview);
            getdata();
            String[] from={"image","text"};
            int[] to={R.id.image,R.id.text};
            adapter=new SimpleAdapter<map<String,Objects>>(mainactivity.this,data_list.R.layout.item,from,to);
            gv.setAdapter(adapter);
            
        }
        public List<Map<String,Objects>> getdata(){
            for(int i=0;i<image.length;i++){
                Map<String,objects> map=new HashMap<String,Objects>();
                map.put("image",image[i]);
                map.put("text",name[i]);
                data_list.add(map);
                }
                return data_list;
            }
    }

    补充关于hashmap:
    这里使用hashmap就相当于在自定义的listview中的自定义泛型 fruit目的都是为了能够容纳一对对的数据集合
      Map<String, Object> map = new HashMap<String, Object>();
                map.put("image", icon[i]);
                map.put("text", iconName[i]);
                data_list.add(map);此处的map就是一对数据的集合

  • 相关阅读:
    C++模板学习之优先队列实现
    static 和const分别怎么用,类里面static和const可以同时修饰成员函数吗?
    C和C++的区别
    C++多态例子_虚函数
    转:master公式(主方法)
    C++11最常用的新特性如下
    转:哈夫曼树详解
    linux shell脚本
    linux 正则表达式
    linux shell命令
  • 原文地址:https://www.cnblogs.com/zzy-frisrtblog/p/5327173.html
Copyright © 2020-2023  润新知