android_21_listView_SimpleAdapter
来源:互联网 发布:网络传输大文件 编辑:程序博客网 时间:2024/06/08 18:40
效果:
布局:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sg31.listviewsimpleadapter.MainActivity" > <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
自定义cell:
<?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/iv_photo" android:layout_width="40dp" android:layout_height="40dp" android:src="@drawable/appicon" /> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="名字" android:textSize="22sp" android:layout_gravity="center_vertical" /></LinearLayout>
代码:
package com.sg31.listviewsimpleadapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inflateWithSimpleAdpter(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void inflateWithArrayAdapter() { String[] objects = new String[]{ "beyond", "love", "shuiying" }; ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(new ArrayAdapter<String>(this, R.layout.cell, R.id.tv_name, objects));} public void inflateWithSimpleAdpter() { ListView listView = (ListView) findViewById(R.id.listView); //集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整型,所以使用一个map来封装这两种数据 List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("photo", R.drawable.appicon); map1.put("name", "beyond"); data.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("photo", R.drawable.love); map2.put("name", "love"); data.add(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("photo", R.drawable.shui); map3.put("name", "shuiying"); data.add(map3); listView.setAdapter(new SimpleAdapter(this, data, R.layout.cell, new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));} }
1 0
- android_21_listView_SimpleAdapter
- android中如何实现自定义倒计时时间的窗口或小插件
- centos6.8平台上安装hive(基于Mysql6.5 和hadoop2.7.3伪分布集群下)
- 自定义圆角、圆形、边框View
- VMware10的安装
- Android_RecyclerView
- android_21_listView_SimpleAdapter
- 版本控制 - SVN
- C++数据结构之栈和队列
- Cadence Genus userguide阅读笔记
- ecc算法入门介绍
- 个人记录-LeetCode 60. Permutation Sequence
- Java中的equals和hashCode方法详解
- Java 邮件发送接收POP3ReceiveServiceTest,SendmailServiceTest示例
- Java方法中的参数String... args