[android] 練習使用ListView(一),androidlistview

來源:互聯網
上載者:User

[android] 練習使用ListView(一),androidlistview

練習使用ListView,BaseAdapter,先展示文字的,再練習圖片的

MainActivity.java

package com.android.test;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {    private ListView lv_images;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lv_images = (ListView) findViewById(R.id.lv_images);        ListAdapter adapter = new ImageAdapter();        lv_images.setAdapter(adapter);    }    /**     * 適配器     * @author taoshihan     *     */    class ImageAdapter extends BaseAdapter {        @Override        public View getView(int position, View convertView, ViewGroup parent) {            String url=(String) getItem(position);            View view;            if(convertView!=null){                view=convertView;            }else{                view=View.inflate(getApplicationContext(), R.layout.image_item, null);            }            TextView tv_image=(TextView) view.findViewById(R.id.tv_image);                        tv_image.setText(url);            return view;        }        @Override        public int getCount() {            // TODO Auto-generated method stub            return Images.IMAGE_URLS.length;        }        @Override        public Object getItem(int position) {            return Images.IMAGE_URLS[position];        }        @Override        public long getItemId(int position) {            // TODO Auto-generated method stub            return position;        }    }    static class Images{        public final static String[] IMAGE_URLS = new String[]{            "http://img.my.csdn.net/uploads/201508/05/1438760758_3497.jpg",                "http://img.my.csdn.net/uploads/201508/05/1438760758_6667.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760756_3304.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760755_6715.jpeg",              "http://img.my.csdn.net/uploads/201508/05/1438760726_5120.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760726_8364.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760725_4031.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760724_9463.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760724_2371.jpg",              "http://img.my.csdn.net/uploads/201508/05/1438760707_4653.jpg"        };    }}

activity_main.xml

<?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="wrap_content"    android:orientation="vertical">    <ListView         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/lv_images"></ListView></LinearLayout>

image_item.xml

<?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="wrap_content"    android:orientation="vertical" >    <TextView        android:id="@+id/tv_image"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>

 

聯繫我們

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