您的位置:首页 > 移动开发 > Android开发

android组件ListView之SimpleAdapter简单使用

2017-04-20 20:31 471 查看

SimpleAdapter

java.lang.Object

↳ android.widget.BaseAdapter

↳ android.widget.SimpleAdapter

ArrayAdapter只能适配简单的数据,而SimpleAdapter相对来说可以丰富列表项的内容,比如我们常见的QQ,微信,新闻列表。



SimpleAdapter 只有一个构造函数

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)


从构造函数的参数可以初步理解下参数的意思,data数据、resource列表项的文件、from展示的数据、to数据展示的地方。下面代码中详细说明各个参数的作用。

Activity类

public class MainActivity extends Activity {
private ListView listView;
private List<Map<String, Object>> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv);
initDataList();
// key值数组,适配器通过key值取value,与列表项组件一一对应
String[] from = { "img", "title", "content", "date" };
// 列表项组件Id 数组
int[] to = { R.id.item_img, R.id.item_title, R.id.item_content,
R.id.item_date };
/**
* SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
* context:activity界面类
* data 数组内容是map的集合数据
* resource 列表项文件
* from map key值数组
* to 列表项组件id数组      from与to一一对应,适配器绑定数据
*/
final SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.listview_item, from, to);

listView.setAdapter(adapter);
/**
* 单击
*/
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Map<String, Object> map = list.get(arg2);
String str = "";
str += map.get("title") + "---" + map.get("content") + "\n"
+ "长按删除!";
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT)
.show();

}
});
/**
* 长按
*/
listView.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
list.remove(arg2);
adapter.notifyDataSetChanged();// 更新列表数据
Toast.makeText(MainActivity.this, "删除成功!", Toast.LENGTH_SHORT)
.show();
return false;
}
});

}

/**
* 初始化适配器需要的数据格式
*/
private void initDataList() {
//图片资源
int img[] = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
R.drawable.i8, R.drawable.i9, R.drawable.i10 };
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", img[i]);
map.put("title", "android" + i);
map.put("content", "SimpleAdapter" + i);
map.put("date",
new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
list.add(map);
}

}

}


主界面activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.l6.MainActivity"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

</ListView>

</LinearLayout>


列表项文件listview_item.xml

<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" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:id="@+id/item_img"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="#F00"
android:textSize="20sp" />

<TextView
android:id="@+id/item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>

<TextView
android:id="@+id/item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="时间"
android:textColor="#0FF"
android:textSize="16sp" />

</RelativeLayout>


效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐