Use of the Spinner, GridView, and Gallery controls

Source: Internet
Author: User

In the previous article, we only introduced one of AdapterView's ListView. In this article, I will explain and explain the other three.


1. Spinner drop-down list)

1) MainActivity. java

Package com. example. l026_adapterview_other1; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. spinner; public class MainActivity extends Activity {private Spinner sp; private ArrayAdapter aAdapter; private String datas [] = {"standard mode", "mute mode", "vibration mode ", "Custom mode" };@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sp = (Spinner) findViewById (R. id. spinner1); aAdapter = new ArrayAdapter <String> (this, android. r. layout. simple_spinner_item, datas); sp. setAdapter (aAdapter );}}

2) activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <Spinner        android:id="@+id/spinner1"        android:layout_width="match_parent"        android:layout_height="wrap_content" />                                                                                                 </LinearLayout>

3) Running Effect

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/105934BT-0.jpg "title =" Capture. JPG "/>

Click the drop-down list:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1059345013-1.jpg "title =" Capture 1.JPG"/>



2. GridView mesh)

1) MainActivity. java

package com.example.l027_adapterview_other2;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.widget.GridView;import android.widget.SimpleAdapter;public class MainActivity extends Activity {    private GridView gv;    private SimpleAdapter sAdapter;    private int[] image={R.drawable.dice_1,R.drawable.dice_2,            R.drawable.dice_3,R.drawable.dice_4,R.drawable.dice_5,            R.drawable.dice_6,R.drawable.dice_action_0,R.drawable.dice_action_1,            R.drawable.dice_action_2,R.drawable.dice_action_3,R.drawable.icon_002,            R.drawable.icon_007,R.drawable.icon_010,R.drawable.icon_012,            R.drawable.icon_012,R.drawable.icon_013,R.drawable.icon_018,            R.drawable.icon_019,R.drawable.icon_019_cover,R.drawable.icon_020,            R.drawable.icon_021,R.drawable.icon_021_cover,R.drawable.icon_022_cover,            R.drawable.icon_022,R.drawable.icon_024,R.drawable.icon_024_cover,            R.drawable.icon_027,R.drawable.icon_027_cover,R.drawable.icon_029,            R.drawable.icon_029_cover,R.drawable.icon_030,R.drawable.icon_030_cover,            R.drawable.icon_035,R.drawable.icon_035_cover,R.drawable.icon_040,            R.drawable.icon_040_cover};    private String[] name={""};    private List<Map<String,Object>> lists;    private Map<String,Object> maps;                                                                    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        gv=(GridView)findViewById(R.id.gridView1);        lists=new ArrayList<Map<String,Object>>();        for(int i=0;i<image.length;i++){            maps=new HashMap<String,Object>();            maps.put("image", image[i]);            lists.add(maps);        }        sAdapter=new SimpleAdapter(this, lists, R.layout.cell,                new String[]{"image"}, new int[]{R.id.iv});        gv.setAdapter(sAdapter);    }}

2) MyAdapter. java

package com.example.l027_adapterview_other2;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.Gallery.LayoutParams;import android.widget.ImageView;public class MyAdapter extends BaseAdapter{    private Context context;    private int image[]={};    public MyAdapter(Context context,int[] image){        this.context=context;        this.image=image;    }                                                                 @Override    public int getCount() {        return this.image.length;    }    @Override    public Object getItem(int position) {        return this.image[position];    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ImageView iv=new ImageView(context);        iv.setBackgroundResource(image[position]);        iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,                LayoutParams.FILL_PARENT));        return iv;    }}



3. Gallery

1) MainActivity. java

Package com. example. l028_adapterview_gallery; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gallery; import android. widget. toast; public class MainActivity extends Activity {private Gallery gy; private MyAdapter mAdapter; private int [] image = {R. drawable. c0, R. d Rawable. c1, R. drawable. c2, R. drawable. c3, R. drawable. c4, R. drawable. c5, R. drawable. c6, R. drawable. c7, R. drawable. c8, R. drawable. c9, R. drawable. c10, R. drawable. c11, R. drawable. c12, R. drawable. c13, R. drawable. c14, R. drawable. c5}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); gy = (Gallery) findViewById (R. id. gallery_main ); MAdapter = new MyAdapter (this, image); gy. setAdapter (mAdapter); gy. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Toast. makeText (MainActivity. this, "already set as Wallpaper", Toast. LENGTH_SHORT ). show ();}});}}

2) MyAdapter. java

package com.example.l028_adapterview_gallery;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.Gallery.LayoutParams;import android.widget.ImageView;public class MyAdapter extends BaseAdapter{    private Context context;    private int image[]={};    public MyAdapter(Context context,int[] image){        this.context=context;        this.image=image;    }          @Override    public int getCount() {        return this.image.length;    }    @Override    public Object getItem(int position) {        return this.image[position];    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ImageView iv=new ImageView(context);        iv.setBackgroundResource(image[position]);        iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,                LayoutParams.FILL_PARENT));        return iv;    }}


This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1281691

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.