概述
ArrayAdapter是BaseAdapter的派生类,在BaseAdapter的基础上,添加了一项重大的功能:可以直接使用泛型构造。这里展示一个最简单的使用ArrayAdapter的例子。
源代码
MainActivity
package com.example.administrator.listview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//设置3个按钮
Button btnArray,btnSimple,btnBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到xml中的按钮
btnArray = ((Button) findViewById(R.id.array_adapter));
btnSimple = ((Button) findViewById(R.id.simple_adapter));
btnBase = ((Button) findViewById(R.id.base_adapter));
//设置监听事件
btnArray.setOnClickListener(this);
btnSimple.setOnClickListener(this);
btnBase.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
//按下第一个按钮的情况
case R.id.array_adapter:
Intent intent = new Intent(this,ArrayActivity.class);
startActivity(intent);
break;
}
}
}
ArrayActivity
新建一个Activity,命名为ArrayActivity
package com.example.administrator.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ArrayActivity extends AppCompatActivity {
//定义ListView 变量
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array);
//获取ListView
listView = ((ListView) findViewById(R.id.listview));
//new一个ArrayList变量,叫做name,当作传入参数
ArrayList<String> names = new ArrayList<>();
names.add("john");
names.add("john2");
names.add("john3");
names.add("john4");
names.add("john5");
names.add("john6");
names.add("john7");
//新建ArrayAdapter类型的adapter变量,要把相关的东西都传给构造函数,如布局文件、一个textview,一个ArrayList等
//使用系统提供的布局simple_list_item_1,注意在R之前加上android
//ArrayAdapter<String> adapater = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,names);
//使用自己定义的布局array_item,注意R之前不要加上android
ArrayAdapter<String> adapater = new ArrayAdapter<String>(this,R.layout.array_item,R.id.mytextview,names);
//设置Adapter,使adapater与listView关联
listView.setAdapter(adapater);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.listview.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/array_adapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ArrayAdapt"
/>
<Button
android:id="@+id/simple_adapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SimpleAdapt"
/>
<Button
android:id="@+id/base_adapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BaseAdapt"
/>
</LinearLayout>
activity_array.xml
这是listview的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.listview.ArrayActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</RelativeLayout>
array_item.xml
新建一个xml文件,命名为array_item,这是传给adaper的构造函数用的。
<?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="match_parent">
<TextView
android:id="@+id/mytextview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="john"
android:textColor="#ff0000" />
</LinearLayout>