views:

116

answers:

1

I'm just getting my feet wet with Android and have built a UI that contains a TabHost with three tabs. Each tab is powered by its own Activity. The first Tab contains a listview with a prepopulated set of rows and is built from a custom ArrayAdapter.

The problem I'm running into is that none of the ListView rows are tappable. In other words, when I tap on them there is no orange selection. If I use the scroll ball on my Nexus One it will select, but any touch gestures don't seem to be responding.

All the UI is being handled using XML files with a main.xml housing the TabHost -> LinearLayout -> TabWidget/FrameLayout and a nearby_activity.xml file containing my 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>

And the relevant code from my Activity that is set to show in the selected tab.

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;
    }
    }
}

My listview row's are populated by an XML file as well.

<?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>

Thanks for any help you can offer.

A: 

You want a Listview tappable, well you need implement the method 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();
    }

and to ensure the orange colour you must set the property

android:focusable="false"

in your Listview Row.xml

Jorgesys
That was it. Thanks.
Justin Williams