ListView控件:(2)SimpleAdapter适配器生成

来源:互联网 发布:手机棋牌软件开发 编辑:程序博客网 时间:2024/05/16 11:19

1.java代码

package com.yun.activity;import java.util.ArrayList;import java.util.HashMap;import com.example.yunsheng.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MySimpleAdapter extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_simpleadapter);ListView lv = (ListView) findViewById(R.id.lv);/* 定义一个动态数组 */ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();/* 在数组中存放数据 */for (int i = 0; i < 10; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put("userId", "第" + i + "行");map.put("userName", "这是第" + i + "行");map.put("userImage", R.drawable.ic_ipod);// 加入图片listItem.add(map);}SimpleAdapter mSimpleAdapter = new SimpleAdapter(this, listItem,// 需要绑定的数据R.layout.simpleadapter_item,// 每一行的布局// 动态数组中的数据源的键对应到定义布局的View中new String[] { "userId", "userName", "userImage" }, new int[] {R.id.userId, R.id.userName, R.id.userImage });lv.setAdapter(mSimpleAdapter);// 为ListView绑定适配器lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubsetTitle("你点击了第" + arg2 + "行");// 设置标题栏显示点击的行HashMap item = (HashMap) arg0.getItemAtPosition(arg2);String section = String.valueOf(item.get("userName").toString());// get每一行的数据的名字Toast.makeText(MySimpleAdapter.this, section, Toast.LENGTH_LONG).show();}});}}


2.配置文件

/Yunsheng/res/layout/activity_simpleadapter.xml

    
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="fill_parent"    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"     android:layout_height="wrap_content"    android:id="@+id/myListItem"     android:paddingBottom="3dip"    android:paddingLeft="10dip"> <!-- 添加一个ListView控件 --><ListView    android:id="@+id/lv"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    />   </LinearLayout>




/Yunsheng/res/layout/simpleadapter_item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TextView            android:id="@+id/userId"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textSize="20dp" >        </TextView>        <TextView            android:id="@+id/userName"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textColor="#F00F11" >        </TextView>    </LinearLayout>    <ImageView        android:id="@+id/userImage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:rotationX="30" /></RelativeLayout>

3.执行结果










0 0
原创粉丝点击