SimpleAdapter 的应用day5

//SimpleAdapter -- 自定义下拉列表android

//下拉列表里面 有:每一张图片对应一个数据
//格式数组

       图片  x
       图片  xx
       图片  xxx
//选中完 后 在 自定义的下拉列表下面的
//TextView控件 显示选中的数据(图片不用显示)ide

一、在res/layout里面布局界面 有2个.xml文件布局

//第一个.xml文件有一个Spinner 控件和 TextViewthis

//第二个.xml文件时本身 自定义的下拉列表布局
//这样就不用用android定义的下拉列表了xml

//activity_main.xml 代码对象

<LinearLayout 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:orientation="vertical" >图片

   
   
    <Spinner
        android:id="@+id/simp_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />utf-8

</LinearLayout>ci

----------------------------

//simpletespinner.xml 代码

<?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="horizontal" >
   
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:scaleType="centerInside"
       />
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"放
        android:layout_height="wrap_content"
        android:gravity="center"/>

</LinearLayout>
----------------------

二、在res/drawable里面放入须要的图片

-----------------------

三、在MainActivity里 实现功能

代码

public class MainActivity extends Activity {
//声明Spinner 对象
private Spinner spinner;
//声明自定义 适配器对象
private SimpleAdapter simp_adapter;
private TextView textview;
//要用的数据 用数组 装起来
private String[] data = {"大人","小孩","青少年","老年人"};
private int[] image = {R.drawable.bump,R.drawable.calculator,
  R.drawable.music,R.drawable.radio};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  spinner = (Spinner) this.findViewById(R.id.simp_spinner);
  textview = (TextView) this.findViewById(R.id.textview);
  
  List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
//用循环 将 数据与图片 装进map里 而后 在把map 当作一个对象 一个对象的 装进List里
  for(int i = 0;i<data.length;i++){
   Map<String,Object> map = new HashMap<String, Object>();
   map.put("data", data[i]);
   map.put("image", image[i]);
   list.add(map);
  }
//new 一个适配器
//第二个参数 -- 要放到自定义 下拉列表的数据
//第三个参数 -- 本身定义的布局.xml文件 名字
//第四个参数 -- 字符串数组 -- 里面放map的键
//第五个参数 -- 整型数组 -- 里面放res/layout本身定义的.xml文件 里面 控件的id
  simp_adapter = new SimpleAdapter(this, list, R.layout.simpletespinner,
new String[] {"image","data"}, new int[] {R.id.imageview,R.id.textview});
  
  spinner.setAdapter(simp_adapter);
  
  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    // TODO Auto-generated method stub
//要设置 TextView 就要获取 自定义下拉列表 选中的 数据 的id
//而后经过这个id去获取键的 -- 值 -- 这样就能够 设置了
    Map<String,Object> m = (Map<String, Object>) spinner.getSelectedItem();
   //Map<String, Object> m = (Map<String, Object>) simp_adapter.getItem(position);
    textview.setText(m.get("data") + "");
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub 
   }
  });
 }
}

------------------------------------------------------------

AutocompleteTextView 控件的使用day5.1

//pull解析
//输入一个字 自动检索全部城市

一、在res/layout布局一个AutoCompleteTextView 控件

代码

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

    <AutoCompleteTextView
        android:id="@+id/autocompletetextview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
        android:hint="请输入城市名"
         />

</RelativeLayout>
---------------------
二、在res/layout放一个须要解析的.xml文件

.xml文件如

<?xml version="1.0" encoding="UTF-8"?>
<citylist>
 <p p_id = "01">
 <pn>北京</pn>
 <c c_id="0101"><cn>北京</cn>
  <d d_id="101010100">北京</d>
  <d d_id="101010200">海淀</d>
  <d d_id="101010300">朝阳</d>
  <d d_id="101010400">顺义</d>
  <d d_id="101010500">怀柔</d>
  <d d_id="101010600">通州</d>
  <d d_id="101010700">昌平</d>
  <d d_id="101010800">延庆</d>
  <d d_id="101010900">丰台</d>
  <d d_id="101011000">石景山</d>
  <d d_id="101011100">大兴</d>
  <d d_id="101011200">房山</d>
  <d d_id="101011300">密云</d>
  <d d_id="101011400">门头沟</d>
  <d d_id="101011500">平谷</d>
 </c>
 </p>
</citylist>

--------------------
三、而后在MainActivity 实现 自动检索功能

代码

public class MainActivity extends Activity {

//声明自动填充 控件的对象private AutoCompleteTextView auto;//声明一个适配器private ArrayAdapter<String> c_adapter; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);//利用getResources方法.getXml方法来获取res/目录下的xml文件   XmlPullParser parser = getResources().getXml(R.layout.citys_weather);//AutoCompleteTextView 能够自动 填充//只需把适配器丢给 它的 对象就能够  auto = (AutoCompleteTextView) this.findViewById(R.id.autocompletetextview);  //用一个List集合把城市装起来//定义一个City_data方法 把XmlPullParser 对象传进去 用pull解析//而后 返回一个集合 里面装的是 城市  List<String> city_list = City_data(parser);  c_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,city_list);    auto.setAdapter(c_adapter);   }//pull解析 private List<String> City_data(XmlPullParser parser) {//建立一个List集合 装 城市数据  List<String> c_list = new ArrayList<String>();    try {   int type = parser.getEventType();   while(type != XmlPullParser.END_DOCUMENT){    switch(type){    case XmlPullParser.START_TAG:     if("d".equals(parser.getName())){      c_list.add(parser.nextText());     }     break;    }    type = parser.next();   }  } catch (Exception e) {   // TODO Auto-generated catch block   e.printStackTrace();  }    return c_list; }}

相关文章
相关标签/搜索