ListView看上去不难,用起来也不简单啊.老是出错,今天试了下,把问题解决差不多了.Adapter有三个,至于游标,现在没有使用.在后面文章介绍. 
出现的问题:在LinearLayout 嵌一个ListView,然后在前面放一个TextView结果.只显示TextView的内容.列表没有了--> 
因为LinearLayout是单独显示行或列的.设置android:orientation="vertical"垂直才可以. 
使用ArrayAdapter数据的ListView: 
Java代码 
  1. private ListView listView;  
  2.     private TextView textView;  
  3.     ArrayAdapter<String> arrayAdapter;  
  4.     String[] value={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "};  
  5.     private static String TAG="ListViewTest1";  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState){  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.lv_layout1);  
  11.         init();  
  12.     }  
  13.   
  14.     private void init(){  
  15.         listView=(ListView)findViewById(R.id.listView1);  
  16.         textView=(TextView)findViewById(R.id.lvtv1);  
  17.         arrayAdapter=new ArrayAdapter<String>(this,R.layout.lv_layout1_row,value);  
  18. //这里的布局网络上查或你到Android.jar里找,就会发现都使用android.R.里面的布局,打开源码看到里面只是一个TextView,只显示一列 我把它拿出来单独放在lv_layout1_row,  
  19.         listView.setAdapter(arrayAdapter);  
  20.   
  21.         listView.setOnItemClickListener(new OnItemClickListener(){  
  22.   
  23.             @Override  
  24.             public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3){  
  25.                 setTitle("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());  
  26.                 textView.setText("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());  
  27.             }  
  28.         });  
  29.   
  30.         //添加长时间按下的菜单  
  31.         listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){  
  32.   
  33.             @Override  
  34.             public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){  
  35.                 menu.setHeaderTitle("长按菜单-ContextMenu");  
  36.                 menu.add(0,0,0,"弹出长按菜单0");  
  37.                 menu.add(0,1,0,"弹出长按菜单1");  
  38.             }  
  39.         });         //这里的事件参考别人的.而且这只是控件的上下文菜单,并不能对List选中作出什么东西.如果想实现长按List里面的某一项的话就覆盖OnItemLongClick.和OnItemClick基本相似,返回值不同,返回True表示消耗了事件,就不会传播了,如果返回False.还会到OnItemClick事件处理.  
  40.     }  
  41. 布局:lv_layout1:  
  42. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  43.               android:id="@+id/lv_layout1_ll"  
  44.               android:orientation="vertical"  
  45.               android:layout_width="fill_parent"  
  46.               android:layout_height="fill_parent">  
  47.   
  48.     <TextView android:id="@+id/lvtv1"  
  49.               android:layout_width="fill_parent"  
  50.               android:layout_height="wrap_content"  
  51.               android:text="@string/app_name"  
  52.               android:paddingLeft="12dip"  
  53.               android:paddingRight="12dip"  
  54.               android:background="#00FF00"/>  
  55.   
  56.     <ListView android:layout_width="fill_parent"  
  57.               android:layout_height="wrap_content"  
  58.               android:id="@+id/listView1"/>  
  59.   
  60. </LinearLayout>  
  61. lv_layout1_row:  
  62. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  63.           android:id="@+id/rowtv1"  
  64.           android:layout_width="fill_parent"  
  65.           android:layout_height="wrap_content"  
  66.           android:paddingBottom="4dip"  
  67.           android:paddingLeft="12dip"  
  68.           android:paddingRight="12dip">  
  69. </TextView>  

这样一个自定义列表就完成了. 
SimplaAdapter数据的列表也差不多:只不过这里的列要对应得上,如下面的from和to要对应.掌握这个就容易了, 
Java代码
  1. SimpleAdapter simpleAdapter, simpleAdapter2;  
  2.     public final static String COLUMN_NAME="name";  
  3.     public final static String COLUMN_PHONE="phone";  
  4.     List<Map<String,String>> value, value2;  
  5.     private static String TAG="ListViewTest2";  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState){  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.lv_layout2);  
  11.         init();  
  12.     }  
  13.   
  14.     private void init(){  
  15.         listView=(ListView)findViewById(R.id.listView2);  
  16.         textView=(TextView)findViewById(R.id.lvtv2);  
  17.         button=(Button)findViewById(R.id.listBtn);  
  18.         button.setOnClickListener(new View.OnClickListener(){  
  19.   
  20.             public void onClick(View arg0){  
  21.                 if(listView.getAdapter()==simpleAdapter){  
  22.                     listView.setAdapter(simpleAdapter2);  
  23.                 }else{  
  24.                     listView.setAdapter(simpleAdapter);  
  25.                 }  
  26.             }  
  27.         });  
  28.   
  29.         String[] from={COLUMN_NAME,COLUMN_PHONE};  
  30.         int[] to={R.id.name,R.id.phone};  
  31.         addValue();  
  32.         addValue2();  
  33.   
  34.         simpleAdapter=new SimpleAdapter(this,value,R.layout.lv_layout2_row,from,to);  
  35.         simpleAdapter2=new SimpleAdapter(this,value2,R.layout.lv_layout2_row,from,to);  
  36.   
  37.         listView.setAdapter(simpleAdapter);  
  38.   
  39.         listView.setOnItemClickListener(new OnItemClickListener(){  
  40.   
  41.             @Override  
  42.             public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3){  
  43.                 setTitle("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());  
  44.                 textView.setText("第"+arg2+"个项目"+arg0.getSelectedItem()+" View:"+arg1.toString());  
  45.                 //如果是触摸屏事件,这里的arg0.getSelectedItem()为null,按enter则会显示from中的项.  
  46.             }  
  47.         });  
  48.   
  49.         //添加长时间按下的菜单  
  50.         listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){  
  51.   
  52.             @Override  
  53.             public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){  
  54.                 menu.setHeaderTitle("长按菜单-ContextMenu");  
  55.                 menu.add(0,0,0,"弹出长按菜单0");  
  56.                 menu.add(0,1,0,"弹出长按菜单1");  
  57.             }  
  58.         });  
  59.     }  
  60.   
  61.     //长按菜单响应函数  
  62.     @Override  
  63.     public boolean onContextItemSelected(MenuItem item){  
  64.         setTitle("点击了长按菜单第"+item.getItemId()+"个项目");  
  65.         return super.onContextItemSelected(item);  
  66.     }  
  67.   
  68.     public List<Map<String,String>> addValue(){  
  69.         value=new ArrayList<Map<String,String>>();  
  70.   
  71.         Map<String,String> item1=new HashMap<String,String>();  
  72.         item1.put(COLUMN_NAME,"griffin");  
  73.         item1.put(COLUMN_PHONE,"132123");  
  74.         value.add(item1);  
  75.   
  76.         Map<String,String> item2=new HashMap<String,String>();  
  77.         item2.put(COLUMN_NAME,"eoe.android");  
  78.         item2.put(COLUMN_PHONE,"132");  
  79.         value.add(item2);  
  80.   
  81.         Map<String,String> item3=new HashMap<String,String>();  
  82.         item3.put(COLUMN_NAME,"gryphone");  
  83.         item3.put(COLUMN_PHONE,"132342");  
  84.         value.add(item3);  
  85.   
  86.         return value;  
  87.     }  
  88.   
  89.     public List<Map<String,String>> addValue2(){  
  90.         value2=new ArrayList<Map<String,String>>();  
  91.   
  92.         Map<String,String> item1=new HashMap<String,String>();  
  93.         item1.put(COLUMN_NAME,"google");  
  94.         item1.put(COLUMN_PHONE,"123456");  
  95.         value2.add(item1);  
  96.   
  97.         Map<String,String> item2=new HashMap<String,String>();  
  98.         item2.put(COLUMN_NAME,"android");  
  99.         item2.put(COLUMN_PHONE,"456789");  
  100.         value2.add(item2);  
  101.   
  102.         Map<String,String> item3=new HashMap<String,String>();  
  103.         item3.put(COLUMN_NAME,"netbeans");  
  104.         item3.put(COLUMN_PHONE,"147258");  
  105.         value2.add(item3);  
  106.   
  107.         return value2;  
  108.     }  
  109. 布局:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  110.               android:id="@+id/lv_layout2_ll"  
  111.               android:orientation="vertical"  
  112.               android:layout_width="fill_parent"  
  113.               android:layout_height="fill_parent">  
  114.   
  115.     <Button android:id="@+id/listBtn"  
  116.             android:layout_width="fill_parent"  
  117.             android:layout_height="wrap_content"  
  118.             android:text="@string/list_btn_label"/>  
  119.   
  120.     <TextView android:id="@+id/lvtv2"  
  121.               android:layout_width="fill_parent"  
  122.               android:layout_height="wrap_content"  
  123.               android:text="@string/app_name"  
  124.               android:paddingLeft="12dip"  
  125.               android:paddingRight="12dip"  
  126.               android:background="#FF0000"/>  
  127.   
  128.     <ListView android:layout_width="fill_parent"  
  129.               android:layout_height="wrap_content"  
  130.               android:id="@+id/listView2"/>  
  131.   
  132. </LinearLayout>  
  133. ............  
  134. <RelativeLayout  
  135.     android:id="@+id/lv_layout2_rl"  
  136.     android:layout_width="fill_parent"  
  137.     xmlns:android="http://schemas.android.com/apk/res/android"  
  138.     android:layout_height="wrap_content"  
  139.     android:paddingBottom="4dip"  
  140.     android:paddingLeft="12dip"  
  141.     android:paddingRight="12dip">  
  142.   
  143.     <TextView android:id="@+id/name"  
  144.               android:paddingTop="12dip"  
  145.               android:layout_width="fill_parent"  
  146.               android:layout_height="wrap_content"  
  147.               android:text="@string/app_name"/>  
  148.   
  149.     <TextView android:id="@+id/phone"  
  150.               android:paddingTop="32dip"  
  151.               android:layout_width="fill_parent"  
  152.               android:layout_height="wrap_content"  
  153.               android:text="@string/app_name"/>  
  154.   
  155. </RelativeLayout>  

ListView的简单应用就是这些了.而通常用ListView是不会这样处理的,而更关注它的Adapter重写,在后面的文章都有使用到. 
posted on 2011-03-09 10:38  kitea  阅读(1036)  评论(0)    收藏  举报