Android -- ListView與Adapter

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   http   

ListView在Android中有著很重要的作用。Android開發中ListView是比較常用的組件,它以列表的形式展示具體內容,並且能夠根據資料的長度自適應顯示。

背景                                                                                          

建了個Person類,裡面有Name,Number,id,三個屬性。

private String name;    private String number;    private int id;

主要用來向listView中添加資訊的。

布局                                                                                            

<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"    tools:context=".MainActivity"     android:orientation="vertical">    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent"         /></LinearLayout>

直接放listview上去就OK了。

程式                                                                                           

private ListView lv;
   private List<Person> list;

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        list = new ArrayList<Person>();        lv = (ListView) findViewById(R.id.lv);        addPerson();        lv.setAdapter(new MyAdapter());    }    private class MyAdapter extends BaseAdapter {        @Override        public int getCount() {            //返回大小            return list.size();        }        @Override        public Object getItem(int position) {            // TODO 自動產生的方法存根            return null;        }        @Override        public long getItemId(int position) {            // TODO 自動產生的方法存根            return 0;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            TextView tv = new TextView(getApplicationContext());            tv.setTextSize(50);            tv.setTextColor(Color.BLUE);            Person person = list.get(position);            tv.setText(person.toString());            System.out.println("返回位置"+position);            return tv        }    }    // 添加資料函數    private void addPerson() {        for (int i = 0; i < 20; i++) {            Person person1 = new Person("張三" + i, "12345678912", i);            list.add(person1);        }    }

要申明一個adapter,adapter裡面放資料,然後listview通過setAdapter配置adapter。

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

如果需要自訂lixtview中當樣式的話,可以仙劍一個布局item的布局。

item布局                                                                                  

<?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="60dip"    android:orientation="horizontal" >    <TextView        android:id="@+id/tv_id"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="5dip"        android:text="id"        android:textColor="#ff0000"        android:textSize="18sp" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="vertical" >                <TextView             android:id="@+id/tv_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dip"            android:text="名字"            android:textColor="#000000"            android:textSize="18sp"/>        <TextView             android:id="@+id/tv_phone"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dip"            android:text="電話"            android:textColor="#88000000"            android:textSize="16sp"/>    </LinearLayout></LinearLayout>

重新寫一下adapter裡面的getView方法:

getView                                                                                    

@Override        public View getView(int position, View convertView, ViewGroup parent) {            Person person = list.get(position);            View view = View.inflate(MainActivity.this, R.layout.listview_item, null);            //找id            TextView tv_id = (TextView) view.findViewById(R.id.tv_id);            tv_id.setText("id:"+person.getId());            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);            tv_name.setText("tv_name:"+person.getName());            TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);            tv_phone.setText("tv_phone:"+person.getNumber());            return view;        }

我是天王蓋地虎的分割線                                                               

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

listview學習.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.