ArrayAdapter:
ArrayAdapter<String>(Context context, int resource, int textViewResourceId, String[] objects)
参数:
1.context:上下文
2.resource:布局文件
3.textViewResourceId:待显示数据textview的资源ID
4.objects:待显示的数据,只能显示一种数据
ListView的ArrayAdapter的使用
代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { private ListView lv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); String[] objects =new String[]{
"小志","小志的老婆","萌萌","小志的儿子"
}; lv=(ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<>(this, R.layout.item_view, R.id.lv_name, objects));
} }
item_view布局文件:
<?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="horizontal" > <ImageView
android:id="@+id/lv_phono"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/img01" /> <TextView
android:id="@+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="名字"
android:textSize="20sp" /> </LinearLayout>
activity_main布局文件:
<RelativeLayout 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" > <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </RelativeLayout>