2

Okay I have been tearing my hair out for the past couple hours here trying to figure out why my custom listview adapter cannot work properly with OnItemClickListener. I have a TextView where I use the DrawableLeft attribute to draw an image to the left of a textview inside of a ListView. Now In my textview I do set the android:focusable and android:focusableInTouchMode as well as the android:clickable all to false and in the root of my listview I set the android:descendantFocusability to blocksDescendants as these are common problems encountered with this exact issue. Now here are the layout files I'm going with:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#363636"
android:dividerHeight="1dp"
android:divider="@drawable/menudivider"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding"
android:descendantFocusability="blocksDescendants" />

This is my TextView Layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="#fff"
    android:typeface="sans"
    android:textSize="20sp"
    android:drawablePadding="10dp"
    android:padding="7dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false" />

I set the DrawableLeft Image programmatically inside of my getView function in my class derived of BaseAdapter. As far as the activity that I am implementing the OnItemClickListener function in it goes a little like this:

public class ActivityBase extends Activity implements OnItemClickListener {

private ListView mSlidinglist = null;

@Override
public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);

              mSlidinglist = (ListView)findViewById(R.id.menu);
              ArrayList<MenuItem> items = new ArrayList<MenuItem>();
              items.add(new MenuItem("Test",R.drawable.test) );
              CustomAdapter adapter = new CustomAdapter(this,items);

              mSlidinglist.setAdapter(adapter);
              mSlidinglist.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show();

     }
  }

Here is my Custom Adapter:

    public class CustomAdapter extends BaseAdapter
     {
       private ArrayList<MenuItem> items;
       private Context mContext;

      public CustomAdapter(Context context, ArrayList<MenuItem> items)
        {
        this.items = items;
        this.mContext = context;
        }

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

        v = vi.inflate(R.layout.list_row, null);
    }

    final MenuItem l = items.get(position);

    if(l != null) {
        TextView title = (TextView) v.findViewById(R.id.menu_list);

        title.setText(l.getTitle());
        title.setCompoundDrawablesWithIntrinsicBounds(l.getImageId(), 0, 0, 0);
    }
    return v;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

}

The MenuItem class is just setters and getters for the fields I need...

I cannot get this to fire off, I don't understand why any help is appreciated if more code is needed just let me know just didn't want to post everything. The subclass of BaseAdapter is very simple it just sets the text of the TextView and the DrawableLeft field using setCompoundDrawablesWithIntrinsicBounds. If anyone could help that would be greatly appreciated.

Brosa
  • 1,169
  • 9
  • 19
  • 1
    Why have you added android:clickable = false for your textview? – Purush Pawar Feb 03 '13 at 10:12
  • Have you tried to debug your code and checked for issue ? – GrIsHu Feb 03 '13 at 10:23
  • @Puru doesn't even matter if I enable it so left it disabled – Brosa Feb 03 '13 at 10:28
  • @Grishu yeah I have, nothing seems wrong to me. Been trying to debug this for the past couple hours. – Brosa Feb 03 '13 at 10:28
  • Can you please post some more code your activity ? – GrIsHu Feb 03 '13 at 10:29
  • @Grishu updated with everything that matters the rest of my activity is inflating the action bar and handling hardware buttons. – Brosa Feb 03 '13 at 10:42
  • Where is your setContentView method and why is the constructor name of your custom adapter doesn't match the class name ?! CustomAdapter/SlidingActivityMenuAdapter – Mr.Me Feb 03 '13 at 10:47
  • @Mr.Me I did edit the class name, I'm using a library from github for an implementation of a sliding menu such as of Facebook or Spotify's so in my Activity I set the content view of the sliding portion of the menu then later on this class is extended and there the actual content view will be set. As well I just changed the name of my class's to make it less confusing. But you could think of it as setContentView(R.layout.menu); which would set it to my ListView. – Brosa Feb 03 '13 at 10:55
  • Ok , what is the content of list_row.xml file ? – Mr.Me Feb 03 '13 at 11:11
  • @Mr.Me just the . Thats all it is is the root element and thats all the file is. – Brosa Feb 03 '13 at 11:13

4 Answers4

0

Ok , I've just tested the code, edited it a little and now it is working.

What I have changed:

   if(l != null) {
    TextView title = (TextView) v; // insted of findView

    title.setText(l.getTitle());
    title.setCompoundDrawablesWithIntrinsicBounds(l.getImageId(), 0, 0, 0);
}
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • Just applied the changes myself and I still got nothing on OnItemClick any other significant changes that you've made? – Brosa Feb 03 '13 at 11:29
  • Are you sure you are setting ContentView at some point in your onCreate method call – Mr.Me Feb 03 '13 at 11:31
  • Yes, I am positive the CustomAdapter is properly displaying all of the list rows they just aren't clickable I just tried putting setOnItemClickListener in onStart() to make sure but still no effect. – Brosa Feb 03 '13 at 11:33
  • I also Added xml tags to list_row file, Please check the following links http://pastebin.com/uDs9cpQ9 , http://pastebin.com/nFfv6h4p – Mr.Me Feb 03 '13 at 11:40
  • Do you mind posting the XML for your ListView, I did have XML tags just didn't post them. Your activity looks similar to mine I'm starting to believe its a bug with the Library because any touch events on my ListView will disregard OnItemClick and to slide back to the main view its a touch event. I think I will have to implement this by doing an onclick event for each TextView. Opinion? – Brosa Feb 03 '13 at 11:47
  • I'm going to file a bug report on Github see what the author thinks. – Brosa Feb 03 '13 at 11:48
  • My listView is similar to the one you have, and before reporting a bug test the code I sent to you and see if it works , then report the bug – Mr.Me Feb 03 '13 at 11:52
  • Yep, using your exact patch on the behindView of this SlidingMenu still creates no touch events on the list row's. I'm going to submit a bug and see what he thinks about it. Thanks allot for your help! – Brosa Feb 03 '13 at 12:02
0

try to change the activity to extend ListActivity.. what is the error in the logcat ?

  • I can't, I must extend this Activity in the Library I'm using for the base Activity. The author provides an example and it works fine for him he is using fragments though. https://github.com/jfeinstein10/SlidingMenu/tree/master/example/src/com/slidingmenu/example – Brosa Feb 03 '13 at 22:35
0

Strangely, I got mine to work when I removed the attributes dealing with focusability and clickability. I don't understand it, but see if that works for you.

Reese
  • 1,746
  • 1
  • 17
  • 40
0

Apply these to the child views

android:focusable="false"
android:focusableInTouchMode="false"

Apply this to Parent Layout

android:descendantFocusability="blocksDescendants"

in your code add these two lines :

 mSlidinglist.setClickable(true)
 mSlidinglist.setEnable(true)
Jai
  • 3,211
  • 2
  • 17
  • 26