listview simpleadapter

来源:互联网 发布:阿里云服务器克隆 编辑:程序博客网 时间:2024/05/16 07:31

android listview


list_items.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/Layout01"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/ItemImage"        android:layout_width="60dip"        android:layout_height="60dip" />    <TextView        android:id="@+id/ItemTitle"        android:layout_width="134dp"        android:layout_height="wrap_content"        android:text="TextView01"        android:textSize="20dip" />    <TextView        android:id="@+id/ItemText"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="TextView01"        android:textSize="20dip" /></LinearLayout>


activity_main.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"    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" >    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true" >    </ListView></RelativeLayout>


MainActivity.java
package com.example.simple;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnCreateContextMenuListener;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemLongClickListener;public class MainActivity extends Activity {private final String TAG="simple";private ListView mListView ;private ArrayList<HashMap<String, Object>> mArrayList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mArrayList = new ArrayList<HashMap<String, Object>>() ;mListView = (ListView) findViewById(R.id.listView1);//每个map包含3个元素,//名字分别是ItemImage,ItemTitle,ItemText//和list_items.xml中的元素名对应//list_items.xml中也有3个元素,元素名同上//即map----list_items.xml//map的容器即mArrayList 和 list_items.xml的容器即mListView对应//添加数据        for(int i=0;i<120;i++)        {        //一个map里面有3项,分别对应        HashMap<String, Object> map = new HashMap<String, Object>();        map.put("ItemImage", R.drawable.ic_launcher);//图像资源的ID        map.put("ItemTitle","itemtitle "+String.valueOf(i));        map.put("ItemText", "itemtext "+String.valueOf(i));        mArrayList.add(map);        }                //生成适配器的Item和map元素对应        SimpleAdapter listItemAdapter = new SimpleAdapter(this,mArrayList,//数据源             R.layout.list_items,//list_items.xml--绑定            //map的子元素 ,分别是ItemImage,ItemTitle,ItemText            //如果map中没有其中某个元素比如ItemTitle,则在R.id.ItemTitle证将不会有东西显示            new String[] {"ItemImage","ItemTitle", "ItemText"},             //list_items.xml的子元素,分别是如下            new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}        );               //添加并且显示        mListView.setAdapter(listItemAdapter);                //单机事件        mListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubHashMap<String, Object> map = mArrayList.get(position);String title =  map.get("ItemTitle").toString();setTitle(title);}});                              //添加长按点击事件        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubHashMap<String, Object> map = mArrayList.get(position);String title =  map.get("ItemTitle").toString();setTitle("long "+title);return false;}        });                      //添加长按点击事件,弹出菜单        mListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {menu.setHeaderTitle("delete me?");   menu.add(0, 0, 0, "Yes");menu.add(0, 1, 0, "Cancel");   }});             }//弹出菜单处理函数@Overridepublic boolean onContextItemSelected(MenuItem item) {Log.i(TAG, "点击了长按菜单里面的第"+item.getItemId()+"个项目");return super.onContextItemSelected(item);}@Overridepublic 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;}}