OPhone开发中经常会用到各种各样的组件,像TextView,Button等等。其中经常会使用到ListView(列表),ListView以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。本篇将由浅入深的介绍几种列表,并着重介绍如何自定义列表。具体的表现形式如图1所示。在OPhone系统中,列表的显示需要三个元素:
- public class List1 extendsActivity {
- private ListVeiw listView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listView=new ListVeiw(this);
- listView.setAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, mStrings));
- setContentVieww(listView);
- }
- private String[] mStrings = {
- "Abbaye de Belloc", "Abbaye du Mont des Cats",
- "Acorn", "Adelost", "Affidelice au Chablis",
- "Aisy Cendre", "Allgauer Emmentaler", "Alverca",
- "Ami du Chambertin", "Anejo Enchilado", "Anneau
- "Aragon", "Ardi Gasna", "Ardrahan", "Armenian
- "Asadero", "Asiago", "Aubisque Pyrenees", "Autun",
- "Babybel", "Baguette Laonnaise", "Bakers", "Bal"};
- }
上面代码首先定义一个ListView类型的View对象,用来显示视图。其次使用ArrayAdapter(数组适配器)顾名思义,需要把数据放入一个数组以便显示。ListView如何显示数组中的数据呢?这就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作。数组适配器的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,有许多这样的文件,可以在OPhone目录的OPhoneSDK_1.5/platforms/android-1.5/data/res/layout下面查看),数组。同时用setAdapter()完成适配的最后工作。
- public class List2 extends Activity {
- private ListView listView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- listView=new ListView(this);
- Cursorc=getContentResolver().query(People.CONTENT_URI,
- null, null, null, null);
- startManagingCursor(c);
- ListAdapter adapter = new SimpleCursorAdapter(this,
- android.R.layout.simple_list_item_1,
- c,
- new String[] {People.NAME} ,
- new int[] {android.R.id.text1});
- listView.setAdapter(adapter);
- setContentView(listView);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <ImageView android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <LinearLayout android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- >
- <TextView android:id="@+id/BigText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFFFFFFF"
- android:textSize="30px"
- />
- <TextView android:id="@+id/LittleText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFFFFFFF"
- android:textSize="15px"
- />
- </LinearLayout>
- </LinearLayout>
java Code
-
- public class List3 extends ListActivity {
- List<Map<String, Object>> list;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- list = getListForSimpleAdapter();
- SimpleAdapter adapter = new SimpleAdapter(this, list,
- R.layout.item,
- new String[] { "BigText", "LittleText", "img" },
- new int[] { R.id.BigText,R.id.LittleText, R.id.img });
- setListAdapter(adapter);
- }
- private List<Map<String, Object>> getListForSimpleAdapter() {
- List<Map<String,Object>> list=newArrayList<Map<String, Object>>(3);
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("BigText", "Android");
- map.put("LittleText", "Google phone.");
- map.put("img", R.drawable.n);
- list.add(map);
- map = new HashMap<String, Object>();
- map.put("BigText", "Lenovo");
- map.put("LittleText", "Ophone");
- map.put("img", R.drawable.o);
- list.add(map);
- map = new HashMap<String, Object>();
- map.put("BigText", "Droid");
- map.put("LittleText", "Motorola");
- map.put("img", R.drawable.droid);
- list.add(map);
- return list;
- }
- }
- <Button android:id="@+id/bt"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="Details"
- android:layout_gravity="bottom|right"
- />
- public class List3 extends ListActivity {
- private String []phone={"Android","Lenovo","Droid"};
- private String []maker={"Google phone","Ophone","Motorola"};
- private int[]imgid={R.drawable.n,R.drawable.o,R.drawable.droid};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- MyAdapter adapter = new MyAdapter(this);
- setListAdapter(adapter);
- }
- public void sdelete(){
- new AlertDialog.Builder(this)
- .setTitle("自定义ListVeiw")
- .setMessage("oh,you clicked the button")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener()
- {
- publicvoid onClick(DialogInterface dialog, intwhButton)
- {
- }
- })
- .show();
- }
- public final class ViewHolder{
- public TextView BigText;
- public TextView LittleText;
- public ImageView img;
- public Button sdelete;
- }
- public class MyAdapter extends BaseAdapter
- {
- private LayoutInflater inflater;
- private Context mcontext=null;//assume 2 data
- public MyAdapter(Context context){
- this.mcontext=context;
- inflater=LayoutInflater.from(mcontext);
- }
- public int getCount() {
- return phone.length;
- }
- public Object getItem(int arg0) {
- return null;
- }
- public long getItemId(int arg0) {
- return 0;
- }
- public View getView(int position, View convertView,
- ViewGroup parent) {
- ViewHolder holder;
- if(convertView==null){
- convertView =inflater.inflate(R.layout.item,null);
- holder=new ViewHolder();
- holder.BigText=(TextView)convertView.findViewById(R.id.BigText
- );
- holder.LittleText=(TextView)convertView.findViewById(R.id.LittleText);
- holder.img=(ImageView)convertView.findViewById(R.id.img);
- holder.sdelete=(Button)convertView.findViewById(R.id.bt);
- holder.BigText.setText(phone[position]);
- holder.LittleText.setText(maker[position]);
- holder.img.setBackgroundResource(imgid[position]);
- convertView.setTag(holder);
- holder.sdelete.setOnClickListener(new Button.OnCli ckListener(){
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- sdelete();
- }
- });
- }else{
- holder=(ViewHolder)convertView.getTag();
- holder.BigText.setText(phone[position]);
- holder.LittleText.setText(maker[position]);
- holder.img.setBackgroundResource(imgid[position]);
- holder.sdelete.setOnClickListener(new
- Button.OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- sdelete();
- }
- });
- }
- return convertView;
- }
- }
- }
下面将对上述代码,做详细的解释,listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到listView的长度(这也是为什么在开始的第一张图特别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。