(1)ListView
实现如下:
MainActivity.java
public class MainActivity extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listview); String[] title = this.getResources().getStringArray(R.array.title); String[] content = this.getResources().getStringArray(R.array.content); int[] image = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; MyAdapter adapter = new MyAdapter(this, image, title, content); lv.setAdapter(adapter); }}
MyAdapter.java
package com.example.listview_adapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseAdapter { private Context context; private int[] image; private String[] title; private String[] content; public MyAdapter(Context context, int[] image, String[] title, String[] content) { // TODO Auto-generated constructor stub this.context = context; this.image = image; this.title = title; this.content = content; } @Override public int getCount() { // TODO Auto-generated method stub return title.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub ViewHolder holder; holder = new ViewHolder(); if (arg1 == null) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); arg1 = inflater.inflate(R.layout.listadapter_item, null); holder.imagView = (ImageView) arg1.findViewById(R.id.img1); holder.tv_title = (TextView) arg1 .findViewById(R.id.listadapter_item_tv_title); holder.tv_content = (TextView) arg1 .findViewById(R.id.listadapter_item_tv_content); arg1.setTag(holder); } else { holder = (ViewHolder) arg1.getTag(); } holder.imagView.setImageResource(image[position]); holder.tv_title.setText(title[position]); holder.tv_content.setText(content[position]); return arg1; } private static class ViewHolder { private ImageView imagView; private TextView tv_title; private TextView tv_content; }}
listadapter_item.xml
activity_main.xml
(2)SpinnerView
MainActivity.java
public class MainActivity extends Activity { private Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner) findViewById(R.id.spinner1); String[] data= this.getResources().getStringArray(R.array.data); ArrayAdapteradapter = new ArrayAdapter (this,android.R.layout.simple_spinner_item,data); spinner.setAdapter(adapter); }
activity_main.xml
(3)GridView
MainActivity.java
public class MainActivity extends Activity { private GridView gView; private ImageView imageView; private List
activity_main.java
testimg.xml
(4)Gallery MainActvity.java
public class MainActivity extends Activity { private Gallery gallery; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery = (Gallery) findViewById(R.id.gallery); final int[] data = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; myAdapter = new MyAdapter(this,data); gallery.setAdapter(myAdapter); }MyAdapter.java
public class MyAdapter extends BaseAdapter { private Context context; private int[] data; public MyAdapter(Context context, int[] data) { this.context = context; this.data = data; } @Override public int getCount() { // TODO Auto-generated method stub return data.length; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub LinearLayout linearLayout = new LinearLayout(context); linearLayout.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); ImageView imageView = new ImageView(context); imageView.setImageResource(data[arg0]); imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); linearLayout.addView(imageView); return linearLayout; } }
activity_main.java