android之ListView(列表)

來源:互聯網
上載者:User

android介面中顯示列表效果的幾種方式:

方式一. 繼承ListActivity,使用Listadapter關鍵代碼如下:

private ListAdapter mListAdapter;private List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);listItems=getListItems();mListAdapter = new ListAdapter(this,listItems);setListAdapter(mListAdapter);}private List<Map<String, Object>> getListItems() {         Map<String, Object> map = new HashMap<String, Object>();         map.put("mytitle","Oracle" );         map.put("myimage", R.drawable.a);         map.put("myimages", R.drawable.rating_5);         listItems.add(map);          map = new HashMap<String, Object>();         map.put("mytitle","Sqlite" );         map.put("myimage", R.drawable.b);         map.put("myimages", R.drawable.rating_5);         listItems.add(map);          map = new HashMap<String, Object>();         map.put("mytitle","MySql" );         map.put("myimage", R.drawable.c);         map.put("myimages", R.drawable.rating_5);         listItems.add(map);            return listItems;}

ListAdapter代碼:

package org.anjoy.act;import java.util.List;import java.util.Map;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ListAdapter extends BaseAdapter {private Context context;//上下文路徑private LayoutInflater inflater;//視圖容器private List<Map<String,Object>> listitems;//item資訊集合public class holderView //自訂控制項集合{private TextView mytitle;private ImageView myimage;private ImageView myimages;}//構造DeviceAdapter方法public ListAdapter(Context context,List<Map<String,Object>> listitems){this.context=context;this.inflater=LayoutInflater.from(context);//建立視圖容器this.listitems=listitems;}public int getCount() {// TODO Auto-generated method stubreturn listitems.size();}public Object getItem(int arg0) {// TODO Auto-generated method stubreturn listitems.get(arg0);}public long getItemId(int arg0) {// TODO Auto-generated method stubreturn arg0;}public View getView(int position, View convertView, ViewGroup parent) {// 自訂視圖holderView mView =null;if(convertView==null){mView=new holderView();//擷取listitem布局檔案convertView=inflater.inflate(R.layout.main, null);//擷取控制項對象mView.mytitle=(TextView)convertView.findViewById(R.id.mytitle);mView.myimage =(ImageView)convertView.findViewById(R.id.myimage);mView.myimages =(ImageView)convertView.findViewById(R.id.myimageview);//設定控制項對象到convertview視圖集合中convertView.setTag(mView);}else{mView =(holderView)convertView.getTag();}//設定item控制項上的值mView.mytitle.setText((String) listitems.get(position).get("mytitle"));            mView.myimage.setBackgroundResource((Integer)listitems.get(position).get("myimage"));            mView.myimages.setBackgroundResource((Integer)listitems.get(position).get("myimages"));            mView.myimage.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubnew AlertDialog.Builder(context)            .setIcon(R.drawable.alert)            .setTitle("溫馨提示")            .setMessage("請您操作看看!")            .setPositiveButton("確定", null)            .show();}});            return convertView;}}

3.方法二:activity繼承Listview,使用SimpleAdapter和布局檔案

關鍵代碼:

@Override public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.main,                new String[]{"mytitle","myimageview","myimage"},                new int[]{R.id.mytitle,R.id.myimageview,R.id.myimage});        setListAdapter(adapter);    }     private List<Map<String, Object>> getData() {         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();         Map<String, Object> map = new HashMap<String, Object>();        map.put("mytitle", "IOS教材");        map.put("myimageview",  R.drawable.rating_5);        map.put("myimage", R.drawable.a);        list.add(map);         map = new HashMap<String, Object>();        map.put("mytitle", "Android教材");        map.put("myimageview",R.drawable.rating_5);        map.put("myimage", R.drawable.b);        list.add(map);         map = new HashMap<String, Object>();        map.put("mytitle", "java教材");        map.put("myimageview", R.drawable.rating_5);        map.put("myimage", R.drawable.c);        list.add(map);                 return list;    }

如上

2. 方式三:activity中建立ListView對象,使用ListAdapter

@Override public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.main,                new String[]{"mytitle","myimageview","myimage"},                new int[]{R.id.mytitle,R.id.myimageview,R.id.myimage});        setListAdapter(adapter);    }     private List<Map<String, Object>> getData() {         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();         Map<String, Object> map = new HashMap<String, Object>();        map.put("mytitle", "IOS教材");        map.put("myimageview",  R.drawable.rating_5);        map.put("myimage", R.drawable.a);        list.add(map);         map = new HashMap<String, Object>();        map.put("mytitle", "Android教材");        map.put("myimageview",R.drawable.rating_5);        map.put("myimage", R.drawable.b);        list.add(map);         map = new HashMap<String, Object>();        map.put("mytitle", "java教材");        map.put("myimageview", R.drawable.rating_5);        map.put("myimage", R.drawable.c);        list.add(map);                 return list;    }

如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.