2010-05-28 84 views
1

我只是讓我的腳溼與Android,並已建立一個包含三個選項卡的TabHost的用戶界面。每個選項卡都由其自己的活動提供支持。第一個Tab包含一個預填充行集的列表視圖,並且是從一個自定義ArrayAdapter構建的。列表查看項目沒有響應水龍頭

我遇到的問題是,沒有一個ListView行是可點擊的。換句話說,當我點擊它們時,沒有橙色選擇。如果我在Nexus One上使用滾動球,它會選擇,但任何觸摸手勢似乎都沒有響應。

所有UI正在使用XML文件用的main.xml住房TabHost處理 - >的LinearLayout - > TabWidget/FrameLayout裏以及含有我的ListView UI

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
    android:id="@+id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="@string/nearby_no_events" 
    /> 

    <ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1.0" 
    android:choiceMode="multipleChoice" 
    android:divider="#d9d9d9" 
    android:dividerHeight="1px"  
    android:cacheColorHint="#eee" 
    /> 
</LinearLayout> 

及相關代碼nearby_activity.xml文件從我設置爲顯示在選定選項卡中的活動中。

public class NearbyActivity extends ListActivity 
{ 
    private ArrayList<Event> m_events = null; 
    private EventAdapter m_adapter = null; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nearby_activity); 

      getEvents(); 
     this.m_adapter = new EventAdapter(this, R.layout.eventrow, m_events); 
     setListAdapter(this.m_adapter); 
    } 

    private void getEvents() 
    { 
     m_events = new ArrayList<Event>(); 

     for (int i = 0; i < 100 ; i++) 
     { 
      Event e = new Event(); 
      e.setEventName("Event " + i); 
      e.setVenueName("Staples Center"); 
      e.setStartDate(new Date()); 
      m_events.add(e); 
     } 
    } 

    private class EventAdapter extends ArrayAdapter<Event> 
    { 
     private ArrayList<Event> items; 

    public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> items) 
    { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 

    @Override 
    public View getView (int position, View convertView, ViewGroup parent) 
    { 
     View v = convertView; 
     if (v == null) 
     { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.eventrow, null); 
     } 


     Event e = items.get(position);   
     if (e != null) 
     { 
     TextView nameText = (TextView)v.findViewById(R.id.eventName); 
     TextView venueNameText = (TextView)v.findViewById(R.id.venueName); 
     if (nameText != null) 
     { 
      nameText.setText(e.getEventName());        
     } 

     if(venueNameText != null) 
     { 
      venueNameText.setText(e.getVenueName()); 
     } 
     } 

     return v; 
    } 
    } 
} 

我的列表視圖行也由XML文件填充。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:orientation="vertical" 
    android:padding="4dip"> 

    <TextView 
    android:id="@+id/eventName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:inputType="text" 
    android:singleLine="true" 
    android:ellipsize="marquee" 
    android:textSize="18sp" 
    android:textColor="#000" 
    /> 

    <TextView 
    android:id="@+id/venueName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/eventName"  
    android:layout_alignParentBottom="true" 
    android:layout_marginRight="55dip"  
    android:singleLine="true" 
    android:ellipsize="end" 
    android:scrollHorizontally="true" 
    android:textSize="13sp" 
    android:textColor="#313131" 
    android:layout_alignWithParentIfMissing="true" 
    android:gravity="center_vertical" 
    />  

</RelativeLayout> 

感謝您提供的任何幫助。

回答

5

你想要一個列表視圖可點擊,那麼你需要實現的方法onListItemClick()

@Override 
    protected void onListItemClick(ListView l, View v, final int position, long id) { 
    super.onListItemClick(l, v, position, id);     
Toast.makeText(this, "This is my row number " + position,Toast.LENGTH_LONG).show(); 
    } 

,並確保橙色必須設置該屬性

android:focusable="false" 

在你的列表視圖Row.xml

+0

就是這樣。謝謝。 – 2010-05-28 23:10:21

相關問題