Android課程設計第四天ListView運用

來源:互聯網
上載者:User

標籤:adapter   and   position   style   alt   create   height   擷取   pre   

注意:課程設計只為完成任務,不做細節描述~ 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3  4     xmlns:tools="http://schemas.android.com/tools" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context=".MainActivity"> 8     <ListView 9         android:id="@+id/listview"10         android:layout_width="match_parent"11         android:layout_height="match_parent"12         android:text="asad"13         >14     </ListView>15 16 </RelativeLayout>
<?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">    <ImageView        android:layout_width="80dp"        android:layout_height="80dp"        android:id="@+id/img_ico"        android:src="@mipmap/x2017612"/>    <TextView        android:layout_width="match_parent"        android:layout_height="50dp"        android:id="@+id/text1"        android:layout_toRightOf="@id/img_ico"        android:text="miemie"        android:textSize="16sp"        android:textColor="#0ccfff"/>    <TextView        android:layout_width="match_parent"        android:layout_height="40dp"        android:id="@+id/text2"        android:layout_below="@id/text1"        android:layout_alignLeft="@id/text1"        android:text="xxxxx"        android:textSize="16sp"        android:textColor="#0ccfff"        /></RelativeLayout>
 1 package com.example.myapplication; 2  3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.ImageView; 9 import android.widget.TextView;10 11 /**12  * Created by 櫻花落舞 on 2017/6/12.13  */14 15 public class MyAdapter extends BaseAdapter {16     private String names[], contents[];17     private int images[];18     //上下文19     private Context context;20 21     public MyAdapter(String names[], String contents[], int images[],Context context) {22         this.names = names;23         this.contents = contents;24         this.images = images;25         this.context=context;26     }27 28     //擷取列表總數29     @Override30     public int getCount() {31         return names.length;32     }33 34     //擷取單個item35     @Override36     public Object getItem(int position) {37         return names[position];38     }39 40     //擷取itemid41     @Override42     public long getItemId(int position) {43         return position;44     }45 46     //得到item視圖47     @Override48     public View getView(int position, View convertView, ViewGroup parent) {49         View view = LayoutInflater.from(context).inflate(R.layout.item,null);50         ImageView image = (ImageView) view.findViewById(R.id.img_ico);51         TextView name = (TextView) view.findViewById(R.id.text1);52         TextView content = (TextView) view.findViewById(R.id.text2);53 54         image.setImageResource(images[position]);55         name.setText(names[position]);56         content.setText(contents[position]);57 58         return view;59     }60 }
 1 package com.example.myapplication; 2  3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.ListView; 7 /* 8     listview的使用 9         在布局中準備控制項10         在java代碼中聲明執行個體化11         準備資料來源12         建立適配器13         載入適配器14 15  */16 public class MainActivity extends AppCompatActivity {17     //聲明控制項18     private ListView listView;19 20     @Override21     protected void onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23         setContentView(R.layout.activity_main);24         listView= (ListView) findViewById(R.id.listview);25      26         String names[]={"xh","xhh","xhhh","xhhhh"};27         String contents[]={"Max","max","min","Min"};28         int images[]={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d};29         MyAdapter adapter =new MyAdapter(names,contents,images,MainActivity.this);30         listView.setAdapter(adapter);31     }32 }

 

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.