2010-05-28 37 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

相关问题