我没有用elipse自带的ListView. 自己做了一个
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout   
    android:id="@+id/LinearLayout01"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    xmlns:android="http://schemas.android.com/apk/res/android">  
    
<TextView 
    android:text="TextView01" 
    android:layout_height="wrap_content" 
    android:textSize="20dip" 
    android:layout_width="wrap_content" 
    android:layout_marginRight="100dp"
    android:id="@+id/ItemTitle"
    />
<TextView 
android:text="TextView02" 
android:layout_height="wrap_content" 
android:textSize="20dip" 
android:layout_width="wrap_content" 
android:layout_below="@+id/ItemTitle" 
android:id="@+id/ItemText"
/>
</LinearLayout>  //绑定Layout里面的ListView
        ListView list = (ListView) findViewById(R.id.ListView01);
        
        //生成动态数组,加入数据  
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        for(int i=0; i<4; i++)
        {
         HashMap<String, Object> map = new HashMap<String,Object>();
         map.put("ItemText", "hello");
         map.put("ItemTile", "Level" + i);
         listItem.add(map);
        }
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdatperAdapter = new SimpleAdapter(this, listItem, 
         R.layout.list_items, 
         new String[]{"ItemText","ItemTile"},
         new int[] {R.id.ItemTitle,R.id.ItemText}
         );
        //添加并显示
        list.setAdapter(listItemAdatperAdapter);
        
        //添加点击  
        list.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
         long arg3) {
         // TODO Auto-generated method stub
         setTitle("点击第"+arg2+"个项目");
        }
});
现在显示工作OK. 我想增加一个功能就是选中该行时候, 该行处于被选择状态, 比如该行背景为灰色.

解决方案 »

  1.   

    你看下apidemo里面有一个例子符合的你的要求
    /**
     * A list view where the last item the user clicked is placed in
     * the "activated" state, causing its background to highlight.
     */
    public class List17 extends ListActivity {    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        // Use the built-in layout for showing a list item with a single
            // line of text whose background is changes when activated.
            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_activated_1, mStrings));
            getListView().setTextFilterEnabled(true);
            
            // Tell the list view to show one checked/activated item at a time.
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            
            // Start with first item activated.
            // Make the newly clicked item the currently selected one.
            getListView().setItemChecked(0, true);
        }    @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // Make the newly clicked item the currently selected one.
            getListView().setItemChecked(position, true);
        }    private String[] mStrings = Cheeses.sCheeseStrings;
    }
      

  2.   

    getListView().setItemChecked(position, true); 
    我增加这条. 编译报错. 请问如何解决
      

  3.   

    在你的代码里面应该是
    getListView().setItemChecked(arg2, true); 
      

  4.   

            //添加点击  
            list.setOnItemClickListener(new OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
             long arg3) {
             // TODO Auto-generated method stub
             setTitle("点击第"+arg2+"个项目");  
             getListView().setItemChecked(arg2, true);
             }
    });
    报错:
    The method getListView()is undefined for the type new AdapterView.OnItemClickListener()是不是我加错地方了.
      

  5.   

    用selector去根据不同状态设置背景色