ArrayAdapter:
ArrayAdapter<String>(Context context, int resource, int textViewResourceId, String[] objects)
参数:
1.context:上下文
2.resource:布局文件
3.textViewResourceId:待显示数据textview的资源ID
4.objects:待显示的数据,只能是String
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>
时间: 2024-09-30 05:33:19