Android中有一些View是包含多个元素的,例如ListView,GridView等,为了给View的每一个元素都设置数据,就需要Adapter了。
常用的Adapter包括ArrayAdapter和SimpleAdapter。
ArrayAdapter
ArrayAdapter比较简单,它只能用于显示文字。看代码:
MainActivity.java
package cn.lixyz.listviewtest; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ private ListView listView; private String[] data = {"橘子","苹果","香蕉","西瓜","樱桃","猕猴桃","木瓜","山竹",
"桃","葡萄","山楂","杏","核桃","哈密瓜","菠萝","柚子","无花果","柠檬","李子"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data); listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = listView.getItemAtPosition(position) + "";
Toast.makeText(this,"position=" + position + " text=" + text,Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this,ShowFruit.class);
intent.putExtra("fruitNum",position);
startActivity(intent);
}
}
activity_layout.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"> <ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
> </ListView> </LinearLayout>
ShowFruit.java
package cn.lixyz.listviewtest; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView; public class ShowFruit extends AppCompatActivity { private String[] data = {"橘子橘子橘子橘子橘子", "苹果苹果苹果苹果苹果", "香蕉香蕉香蕉香蕉香蕉",
"西瓜西瓜西瓜西瓜西瓜", "樱桃樱桃樱桃樱桃樱桃", "猕猴桃猕猴桃猕猴桃猕猴桃猕猴桃",
"木瓜木瓜木瓜木瓜木瓜", "山竹山竹山竹山竹山竹", "桃桃桃桃桃", "葡萄葡萄葡萄葡萄葡萄",
"山楂山楂山楂山楂山楂", "杏杏杏杏杏", "核桃核桃核桃核桃核桃", "哈密瓜哈密瓜哈密瓜哈密瓜哈密瓜",
"菠萝菠萝菠萝菠萝菠萝", "柚子柚子柚子柚子柚子", "无花果无花果无花果无花果无花果",
"柠檬柠檬柠檬柠檬柠檬", "李子李子李子李子李子"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_fruit); int i = getIntent().getIntExtra("fruitNum", -1);
((TextView) findViewById(R.id.tv_fruit_desc)).setText(data[i]); findViewById(R.id.bt_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
activity_show_fruit.xml
<LinearLayout 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="cn.lixyz.listviewtest.ShowFruit"
android:orientation="vertical"> <TextView
android:id="@+id/tv_fruit_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60sp"/> <Button
android:id="@+id/bt_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"/>
</LinearLayout>
运行结果:
Simpleadapter
名字叫simple其实并不simple,它可以帮助我们实现更复杂的界面,ListView的大部分应用场景,都可以通过SimpleAdapter来提供列表项。
如图:
实现代码:
MainActivity.java
package cn.lixyz.listviewtest; import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { private ListView listView; private String[] data = {"橘子", "苹果", "香蕉", "西瓜", "樱桃", "猕猴桃", "木瓜", "山竹",
"桃", "葡萄", "山楂", "杏", "核桃", "哈密瓜", "菠萝", "柚子", "无花果", "柠檬", "李子"}; private List<Map<String, Object>> dataList; private String[] from = {"iv_fruit_show", "tv_fruit_name"};
private int[] to = {R.id.iv_fruit_show, R.id.tv_fruit_name}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list_view);
dataList = new ArrayList<Map<String, Object>>();
SimpleAdapter sa = new SimpleAdapter(MainActivity.this, getData(), R.layout.layout_adapter, from, to); listView.setAdapter(sa); listView.setOnItemClickListener(this);
} private List<Map<String, Object>> getData() { List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 18; i++) {
list.add(getIdByName(MainActivity.this, "drawable", "pic" + i));
} for (int i = 0; i < 18; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("iv_fruit_show", list.get(i));
map.put("tv_fruit_name", data[i]);
dataList.add(map);
}
return dataList;
} public static int getIdByName(Context context, String className, String resName) {
String packageName = context.getPackageName();
int indentify = context.getResources().getIdentifier(resName, className, packageName);
return indentify;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ShowFruit.class);
intent.putExtra("index", position);
startActivity(intent);
}
}
activity_layout.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity"> <ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
> </ListView> </LinearLayout>
ShowFruit.java
package cn.lixyz.listviewtest; import android.content.Context;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; import static cn.lixyz.listviewtest.R.drawable.pic0; public class ShowFruit extends AppCompatActivity { private String[] data = {"橘子橘子橘子橘子橘子", "苹果苹果苹果苹果苹果", "香蕉香蕉香蕉香蕉香蕉",
"西瓜西瓜西瓜西瓜西瓜", "樱桃樱桃樱桃樱桃樱桃", "猕猴桃猕猴桃猕猴桃猕猴桃猕猴桃",
"木瓜木瓜木瓜木瓜木瓜", "山竹山竹山竹山竹山竹", "桃桃桃桃桃", "葡萄葡萄葡萄葡萄葡萄",
"山楂山楂山楂山楂山楂", "杏杏杏杏杏", "核桃核桃核桃核桃核桃", "哈密瓜哈密瓜哈密瓜哈密瓜哈密瓜",
"菠萝菠萝菠萝菠萝菠萝", "柚子柚子柚子柚子柚子", "无花果无花果无花果无花果无花果",
"柠檬柠檬柠檬柠檬柠檬", "李子李子李子李子李子"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_fruit); int i = getIntent().getIntExtra("index", -1);
int picId = getIdByName(this, "drawable", "pic" + i); ((ImageView) findViewById(R.id.iv_fruit_show)).setImageResource(picId);
((TextView) findViewById(R.id.tv_fruit_desc)).setText(data[i]); } public static int getIdByName(Context context, String className, String resName) {
String packageName = context.getPackageName();
int indentify = context.getResources().getIdentifier(resName, className, packageName);
return indentify;
}
}
activity_show_fruit.xml
<LinearLayout 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="cn.lixyz.listviewtest.ShowFruit"
android:orientation="vertical"> <ImageView
android:id="@+id/iv_fruit_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <TextView
android:id="@+id/tv_fruit_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
layout_adapter.xml
<?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"
android:orientation="horizontal"
android:weightSum="1"> <ImageView
android:id="@+id/iv_fruit_show"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:src="@drawable/pic0"/> <TextView
android:id="@+id/tv_fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1111"
android:textSize="50dp" /> </LinearLayout>
查看API,发现SimpleAdapter可以将静态数据映射到xml文件定义好的视图中去,你可以指定数据支持的列表如ArrayList组成的Map,在ArrayList中的每个条目对应List中的一行,Maps包含每行数据,你可以指定一个定义了被用于显示行的视图XML文件,通过关键字映射到指定的视图。绑定数据到视图分为两个阶段,首先,如果一个SimpleAdapter.ViewBinder是有效的,setViewValue(android.View.view,Object,String)将被调用。如果返回值是真,绑定成功,如果返回值为假,下面的视图将按照以下顺去去处理:
- l 一个实现了Checkable的视图(例如CheckBox),期望绑定值是一个布尔类型。
- l TextView期望绑定值是一个字符串类型,通过调用setViewText(TextView, String)绑定。
- l ImageView期望绑定值是一个资源id或者一个字符串,通过调用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)。
如果没有一个合适的绑定发生将会抛出IllegalStateException
SimpleAdapter的构造函数
public SimpleAdapter(Context context, List<?extends Map<String,?>> data, int resource, String[] from, int[] to)
参数:
context:关联SimpleAdapter运行着的视图的上下文
data:一个Map列表,在列表中的每个条目对应列表中的一行,应该包含所有在from指定的条目
resource:一个定义列表项目视图布局的资源唯一表示。布局文件将至少应包含哪些定义在to中定义了的名称
from:一个将被添加到Map上关联每一个项目的列名称的列表
to:应该在参数from显示列的视图,这些应该全是TextView。在列表中最初的N视图是从参数from中最初的N列获取的值