Android 學習--ListView 的使用(三)

來源:互聯網
上載者:User

標籤:android

使用SimpleAdapter 建立ListView


程式清單1

<?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">    <ListView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/mylist"        /></LinearLayout>

程式清單2

package xiaocool.net.classspace.ClassBlog;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import xiaocool.net.classspace.R;/** * Created by MRYU on 2015/3/14. */public class TuiJian extends ActionBarActivity {    private ListView listView;    private String[] contents=new String[]{"大聲吼孩子有什麼用?","大聲吼孩子有什麼用?","大聲吼孩子有什麼用?"};    private String[] times=new String[]{"2015/3/14 14:44","2015/3/14 14:44","2015/3/14 14:44"};    private int[] images=new int[]{R.drawable.touxiang,R.drawable.touxiang,R.drawable.touxiang};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.classblog_tuijian);        //建立一個List集合 ,list集合的元素是Map        List<Map<String,Object>> listitems=new ArrayList<Map<String, Object>>();        for(int i=0;i<contents.length;i++){            Map<String,Object> listitem=new HashMap<String,Object>();            listitem.put("content",contents[i]);            listitem.put("time",times[i]);            listitem.put("image",images[i]);            listitems.add(listitem);        }        //建立一個SimpleAdapter        SimpleAdapter simpleAdapter =new SimpleAdapter(this,listitems,R.layout.tuijian_item,                new String[]{"content","time","image"},                new int[]{R.id.textContent,R.id.textTime,R.id.imageView3}                );        listView=(ListView)this.findViewById(R.id.mylist);        listView.setAdapter(simpleAdapter);    }}

程式清單3 每一個清單項目可根據自己需要進行定製

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent"    android:orientation="horizontal"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Text"        android:id="@+id/textContent"        android:textSize="20sp"        android:layout_marginLeft="55dp"        android:layout_marginStart="55dp"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Text"        android:id="@+id/textTime"        android:textSize="15sp"        android:layout_alignBottom="@+id/imageView3"        android:layout_alignLeft="@+id/textContent"        android:layout_alignStart="@+id/textContent" />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/imageView3"        android:src="@drawable/touxiang"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"        android:layout_marginRight="50dp"        android:layout_marginEnd="50dp" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Text"        android:id="@+id/textZan"        android:layout_alignBottom="@+id/imageView3"        android:layout_toRightOf="@+id/textContent"        android:layout_toEndOf="@+id/textContent" /></RelativeLayout>

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5B/41/wKioL1UD5VPx_z8OAAFHvTWthys168.jpg" title="QQ20150314153137.png" alt="wKioL1UD5VPx_z8OAAFHvTWthys168.jpg" />

本文出自 “阿乾-在路上” 部落格,請務必保留此出處http://xiaocool.blog.51cto.com/1593956/1620353

Android 學習--ListView 的使用(三)

聯繫我們

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