Android -- ListView與ArrayAdapter、SimpleAdapter

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   width   

對於ArrayAdapter,裡面雖然能添加圖片,但只能是相同的圖片。

廢話不多說:

布局&&list的item布局                                                                

<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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="wrap_content"         /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

程式                                                                                            

private static String[] names= {"功能1","功能2","功能3","功能4","功能5"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                ListView lv = (ListView) findViewById(R.id.lv);        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.tv, names));    }

--------------------------簡單的分割線--------------0-0-----------------------------------------------------------

SimpleAdapter可以添加圖片的哈~~每一個item都可以加自己想要的圖片。

在item布局裡面給imageView添加id為iv。

程式                                                                                            

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ListView lv = (ListView) findViewById(R.id.lv);        List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();        Map<String,Object> map1 = new HashMap<String, Object>();        map1.put("nameText", "第1個啦啦啦");        map1.put("icon", R.drawable.ic_launcher);        Map<String,Object> map2 = new HashMap<String, Object>();        map2.put("nameText", "第2個啦啦啦");        map2.put("icon", R.drawable.info_buse);        Map<String,Object> map3 = new HashMap<String, Object>();        map3.put("nameText", "第3個啦啦啦");        map3.put("icon", R.drawable.info_duijiao);        Map<String,Object> map4 = new HashMap<String, Object>();        map4.put("nameText", "第4個啦啦啦");        map4.put("icon", R.drawable.info_flash);        data.add(map1);        data.add(map2);        data.add(map3);        data.add(map4);                lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"nameText", "icon"},new int[]{R.id.tv,R.id.iv}));    }

效果:

data為繫結資料,list集合。R.layout.list_item,資料顯示對應的布局,這是要讓資料跟view對象獎勵一個映射關係。from[] ,map集合裡面的資料的key。to[],布局檔案裡面的id。

我是天王蓋地虎的分割線                                                                 

原始碼:http://pan.baidu.com/s/1dD1Qx01

listview學習2.zip

 

 

 

轉載請註明出處:http://www.cnblogs.com/yydcdut

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.