上节学习了适配器ArrayAdapter,这节我们学习适配器重点SimpleAdapter
package com.example.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private LinearLayout myLayout; private ListView myListView; //创建list对象,用来存放列表项每一行的Map信息 List<Map<String,Object>> list=new ArrayList<Map<String,Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myLayout=(LinearLayout)this.findViewById(R.id.mylayout); myListView=new ListView(this); //创建布局参数 LinearLayout.LayoutParams myListViewParams=new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT ); //当拖拽时显示白色,默认是黑色,这里设置白色 myListView.setCacheColorHint(Color.WHITE); //************************ //创建SimpleAdapter对象,有5个参数 //参数1:上下文context //参数2:数据文件 //参数3:每一行的资源布局文件,这里是自定义的列表布局文件,用到了listviewrom.xml //参数4:含义是HashMap中key信息img,name,money,zhe //参数5:是listViewrom.xml中的组件id SimpleAdapter adapter=new SimpleAdapter(this,<span style="color:#FF0000;">getMyData()</span>,<span style="color:#FF0000;">R.layout.listviewrom</span>, new String[]{"img","name","money","zhe"},new int[]{R.id.tripImg,R.id.phoneName,R.id.phoneMoney,R.id.discount}); myListView.setAdapter(adapter);//为myListView添加适配器 //将myListView添加叫流式布局myLayout中 myLayout.addView(myListView,myListViewParams);//把myListView添加到myLayout上 myListView.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0,View arg1,int position,long id) { // TODO 自动生成的方法存根 Toast.makeText(MainActivity.this, list.get(position).get("name").toString(), Toast.LENGTH_SHORT).show(); } }); } @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; } public List<Map<String,Object>> getMyData(){ Map<String,Object> map=new HashMap<String,Object>(); map.put("img",R.drawable.moto); map.put("name","摩托罗拉"); map.put("money","998"); map.put("zhe","5折"); list.add(map); return list; } }
<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=".MainActivity" > <LinearLayout android:id="@+id/mylayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#FFFFF9EB"> </LinearLayout> </RelativeLayout>
<?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="vertical" <span style="color:#FF0000;"> android:listSelector="@drawable/listviewbg" </span> > <ImageView android:id="@+id/tripImg" android:layout_width="68px" android:layout_height="65px" android:layout_margin="10px"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="10px" android:layout_marginRight="10px" android:layout_marginBottom="10px"> <TextView android:id="@+id/phoneName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff000000"/> <TextView android:id="@+id/phoneMoney" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff000000" android:layout_marginTop="5px" android:layout_marginRight="20px"/> <TextView android:id="@+id/discount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffff0000" android:layout_marginTop="5px" android:layout_marginRight="20px"/> </LinearLayout> </LinearLayout>
该xml文件在res下drawable文件夹下,beijing.png也是在该文件夹下,具体selector参照我转载的博客"懒人爱家务_Android中的selector的用法",http://blog.csdn.net/qq_28711703/article/details/51184885
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true" android:drawable="@drawable/beijing" ></item> <item android:state_selected="true" android:drawable="@drawable/beijing" ></item> <item android:state_focused="true" android:drawable="@drawable/beijing" ></item> </selector>