Android開發之ListView-ArrayAdapter的使用


ArrayAdapter:

ArrayAdapter<String>(Context context, int resource, int textViewResourceId, String[] objects)

參數:

1.context:上下文

2.resource:布局文件

3.textViewResourceId:待顯示數據textview的資源ID

4.objects:待顯示的數據,只能顯示一種數據

ListView的ArrayAdapter的使用

代碼:

 1 import android.app.Activity;
 2 import android.os.Bundle;
 3 import android.widget.ArrayAdapter;
 4 import android.widget.ListView;
 5 
 6 public class MainActivity extends Activity {
 7     
 8     private ListView lv;
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         
15         String[] objects =new String[]{
16                 "小志","小志的老婆","萌萌","小志的兒子"
17         };
18         
19         lv=(ListView) findViewById(R.id.lv);
20         lv.setAdapter(new ArrayAdapter<>(this, R.layout.item_view, R.id.lv_name, objects));
21     }
22 
23 }

item_view布局文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:orientation="horizontal" >
 6 
 7     <ImageView
 8         android:id="@+id/lv_phono"
 9         android:layout_width="60dp"
10         android:layout_height="60dp"
11         android:src="@drawable/img01" />
12 
13     <TextView
14         android:id="@+id/lv_name"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:layout_gravity="center_vertical"
18         android:text="名字"
19         android:textSize="20sp" />
20 
21 </LinearLayout>
activity_main布局文件:
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <ListView
 8         android:id="@+id/lv"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" >
11     </ListView>
12 
13 </RelativeLayout>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM