ListView.onItemClick not working

Archie.bpgc picture Archie.bpgc · Aug 3, 2012 · Viewed 12.3k times · Source

My ListView in Activity:

    ListView listView1 = (ListView) menu.findViewById(R.id.menuList);
    String menuItems[] = new String[] { "My Wants", "Profile", "Notifications",
            "Feedback", "Logout" };
    listView1.setAdapter(new SideMenuAdapter(this, menuItems, listView1));
    listView1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            if (position == 1) {
                Intent intent = new Intent(FeedListViewActivity.this,
                        UserProfileActivity.class);
                startActivity(intent);
            }
            if (position == 0) {
                showMyWants();
            }
        }
    });

menu is :

menu = inflater.inflate(R.layout.horz_scroll_menu, null);

horz_scroll_menu.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:background="#FFFFFFFF"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF000000"
        android:text="Menu"
        android:textColor="#FFFFFFFF"
        android:gravity="center" />

    <ListView
        android:id="@+id/menuList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#FFFFFFFF"
        android:scrollbars="none" >
    </ListView>

</LinearLayout>

My SideMenuAdapter:

public class SideMenuAdapter extends BaseAdapter {

    private static final int TYPE_MAX_COUNT = 2;
    private static LayoutInflater inflater = null;
    private Activity activity;
    public ImageLoader imageLoader;
    public static String[] values;
    ListView myList;

    public SideMenuAdapter(Activity a, String[] sa, ListView lv) {

        values = sa;
        activity = a;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myList = lv;
    }

    public int getCount() {

        return values.length;
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {

        return position;
    }

    public static class ViewHolder {

        public TextView mainText;
        public TextView sideText;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = new ViewHolder();
        View vi = convertView;

        LayoutInflater inflater = activity.getLayoutInflater();
        vi = inflater.inflate(R.layout.side_menu_list_item, null);
        holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
        holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);
        vi.setTag(holder);

        holder.mainText = (TextView) vi.findViewById(R.id.mainText_sideMenu);
        holder.sideText = (TextView) vi.findViewById(R.id.sideText_sideMenu);

        holder.mainText.setText(values[position]);
        if (position == 2) {

            holder.sideText.setText("3");
            holder.sideText.setBackgroundResource(R.drawable.orange);
        }
        return vi;
    }

    @Override
    public int getViewTypeCount() {

        return TYPE_MAX_COUNT;
    }

}

My Xml for ListView items:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sideMenuListItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:padding="3dp" >

    <TextView
        android:id="@+id/sideText_sideMenu"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:gravity="center"
        android:textSize="20dp"
        android:padding="5dp" />

    <TextView
        android:id="@+id/mainText_sideMenu"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/sideText_sideMenu"
        android:clickable="true"
        android:gravity="left|center_vertical"
        android:textSize="20dp"
        android:padding="5dp" />

</RelativeLayout>

when running this application on Emulator, if i click with mouse nothing happens. But when i select any item on the list using navigation buttons on the KeyBoard and click Enter, it works fine.

when running the app on a device. If i go on click any item of the list, like some 10-20 times it works sometimes.

Edit:

Actually everything worked fine when i was using predefined ArrayAdapter<String> and android.R.simple_list_item. But i want a custom adapter

why is it so?

Answer

Archie.bpgc picture Archie.bpgc · Aug 3, 2012

Its because in my ListView item layout i added

android:clickable="true"

for both the TextViews. So when i click on the ListView item it indeed is a click on these TextView whose onClick is not implemented. Removing the clickable attribute from the TextViews solved my problem.

Thanks everyone