Android listView 梳理

这几天关于这个ListView一团乱。现在重新梳理下:

①项vlist.xml:

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:orientation=”vertical” android:layout_width=”fill_parent”
  4.     android:layout_height=”fill_parent”>
  5.         <TextView android:id=”@+id/materialName”
  6.             android:layout_width=”wrap_content”
  7.             android:layout_height=”wrap_content”
  8.             android:textSize=”30px” />
  9.         <TextView android:id=”@+id/materialFormat”
  10.             android:layout_width=”wrap_content”
  11.             android:layout_height=”wrap_content”
  12.             android:textSize=”20px” />
  13.         <TextView android:id=”@+id/id”
  14.             android:layout_width=”wrap_content”
  15.             android:layout_height=”wrap_content”
  16.             android:textSize=”20px” />
  17. </LinearLayout>

②页面activity_list.xml:

 

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:layout_width=”match_parent”
  4.     android:layout_height=”match_parent”
  5.     android:orientation=”vertical” >
  6. <ListView android:layout_width=”wrap_content”
  7.           android:layout_height=”wrap_content”
  8.           android:id=”@+id/ListView01″
  9.           />
  10. </LinearLayout>

③ListViewActivity.xml:

 

 

[java][/java] view plaincopy

  1. package cn.com.smartcost.offer.ui;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import cn.com.smartcost.offer.R;
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.OnItemClickListener;
  13. import android.widget.ListView;
  14. import android.widget.SimpleAdapter;
  15. public class ListViewActivity extends Activity {
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_list);
  20.         //绑定Layout里面的ListView
  21.         ListView list = (ListView) findViewById(R.id.ListView01);
  22.         //生成动态数组,加入数据
  23.        List<Map<String, Object>> listItem = new ArrayList<Map<String, Object>>();
  24.         for(int i=0;i<10;i++)
  25.         {
  26.             HashMap<String, Object> map = new HashMap<String, Object>();
  27.             map.put(“materialName”, “Level “+i);
  28.             map.put(“materialFormat”, “Finished in 1 Min 54 Secs, 70 Moves! “);
  29.             listItem.add(map);
  30.         }
  31.         //生成适配器的Item和动态数组对应的元素
  32.         SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源
  33.             R.layout.vlist,//ListItem的XML实现
  34.             //动态数组与ImageItem对应的子项
  35.             new String[] {“materialName”, “materialFormat”},
  36.             //ImageItem的XML文件里面的一个ImageView,两个TextView ID
  37.             new int[] {R.id.materialName,R.id.materialFormat}
  38.         );
  39.         //添加并且显示
  40.         list.setAdapter(listItemAdapter);
  41.         //添加点击
  42.         list.setOnItemClickListener(new OnItemClickListener() {
  43.             @SuppressWarnings(“unchecked”)
  44.             @Override
  45.             public void onItemClick(AdapterView<?> parent, View view, int position,
  46.                     long id) {
  47.                 ListView lView = (ListView)parent;
  48.                  Map<String, Object> map = (Map<String, Object>) lView.getItemAtPosition(position);
  49.                 setTitle(“点击第”+map.get(“materialName”).toString()+”个项目”);
  50.             }
  51.         });
  52.     }
  53.     //长按菜单响应函数
  54.     @Override
  55.     public boolean onContextItemSelected(MenuItem item) {
  56.         setTitle(“点击了长按菜单里面的第”+item.getItemId()+”个项目”);
  57.         return super.onContextItemSelected(item);
  58.     }
  59. }

标签