Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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

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.

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)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top