一般情况下自定义适配器继承自BaseAdapter就够用了,当然Android为了方便懒人,专门扩展了两种简单易用的适配器,如ArrayAdapter用于每行只显示文本的情况,而SimpleAdapter用于每行显示左图标右文本的情况。实际开发中,ArrayAdapter多用于Spinner,但是SimpleAdapter却很少使用。像ListView和GridView一般都是直接使用BaseAdapter,并不使用布局过于简单的SimpleAdapter。
ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, starArray); starAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); Spinner sp = (Spinner) findViewById(R.id.sp_hello); sp.setPrompt("请选择行星"); sp.setAdapter(starAdapter); sp.setOnItemSelectedListener(new MySelectedListener()); private String[] starArray = {"水星", "金星", "地球", "火星", "木星", "土星"}; class MySelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, "您选择的是"+starArray[arg2], Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> arg0) { } }
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="?android:attr/spinnerItemStyle" android:singleLine="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:textAlignment="inherit" android:textSize="17sp" android:textColor="#0000ff" />上面这个xml布局用到了TextView的三个新属性,说明如下:
import java.util.ArrayList; import com.example.exmsimplewidgte.R; import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; @SuppressLint({ "DefaultLocale", "InflateParams" }) public class TitleListAdapter extends BaseAdapter implements OnItemClickListener, OnItemLongClickListener { private ArrayList<String> mTitleList; private LayoutInflater mInflater; private Context mContext; public TitleListAdapter(Context context, String[] title_list) { this.mInflater = LayoutInflater.from(context); mContext = context; mTitleList = new ArrayList<String>(); for (int i=0; i<title_list.length; i++) { mTitleList.add(title_list[i]); } } @Override public int getCount() { return mTitleList.size(); } @Override public Object getItem(int arg0) { return mTitleList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.list_title, null); holder.tv_seq = (TextView) convertView.findViewById(R.id.tv_seq); holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_seq.setText(""+(position+1)); holder.tv_title.setText(mTitleList.get(position)); return convertView; } public final class ViewHolder { public TextView tv_seq; public TextView tv_title; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String desc = String.format("您点击了第%d项,该项的标题是%s", position+1, mTitleList.get(position)); Toast.makeText(mContext, desc, Toast.LENGTH_LONG).show(); } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { String desc = String.format("您长按了第%d项,该项的标题是%s", position+1, mTitleList.get(position)); Toast.makeText(mContext, desc, Toast.LENGTH_LONG).show(); return true; } }
String[] weekArray = {"星期一", "星期二", "星期三", "星期四", "星期五"}; TitleListAdapter titleAdapter = new TitleListAdapter(this, weekArray); ListView lv_hello = (ListView) findViewById(R.id.lv_hello); lv_hello.setFooterDividersEnabled(true); lv_hello.setAdapter(titleAdapter); lv_hello.setOnItemClickListener(titleAdapter); lv_hello.setOnItemLongClickListener(titleAdapter);
String[] yearArray = {"鼠年", "牛年", "虎年", "兔年", "龙年", "蛇年", "马年", "羊年", "猴年", "鸡年", "狗年", "猪年"}; ContentGridAdapter contentAdapter = new ContentGridAdapter(this, yearArray); GridView gv_hello = (GridView) findViewById(R.id.gv_hello); gv_hello.setNumColumns(5); gv_hello.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); gv_hello.setHorizontalSpacing(1); gv_hello.setVerticalSpacing(1); gv_hello.setAdapter(contentAdapter); gv_hello.setOnItemClickListener(contentAdapter); gv_hello.setOnItemLongClickListener(contentAdapter);
点此查看Android开发笔记的完整目录