user1411093
user1411093

Reputation: 23

ListActivity's onListItemClick does not work

In my application with a ListActivity the overwritten onListItemClick callback is not fired by clicking on a ListView item.

I have defined a custom listview with the followin xml Files, permissionview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Apply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_apply"
            android:onClick="onApplyClick" />

        <Button
            android:id="@+id/btn_install"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_install"
            android:onClick="onInstallClick" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />

    </LinearLayout>

    <ListView android:id="@android:id/list" 
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false">

    </ListView>  
</LinearLayout>

and the viewitem layout checkable.xml :

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


   <ImageView android:id="@+id/img" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" android:layout_alignParentBottom="true"
        android:src="@drawable/ic_launcher" android:focusable="false" />

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
    android:layout_marginRight="10px"

    android:clickable="true">

 </CheckBox>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/img"
        android:gravity="bottom"
        android:text="@+id/label"
        android:textSize="15px"/>
</RelativeLayout>

The defined ArrayAdapter is defined as following:

     public class InteractiveArrayAdapter extends ArrayAdapter<Model> {


    private final List<Model> list;
    private final Activity context;
    private boolean changeable;

    public InteractiveArrayAdapter(Activity context, List<Model> list, boolean changeable) {
        super(context, R.layout.checkable, list);
        this.context = context;
        this.list = list;
        this.changeable = changeable;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
        protected ImageView image;
        protected int pos;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
                view = inflater.inflate(R.layout.checkable, null);


            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.image = (ImageView) view.findViewById(R.id.img);
            viewHolder.pos = position;


            view.setClickable(true);

            if (!changeable)viewHolder.checkbox.setEnabled(false);


            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {



                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Model element = (Model) viewHolder.checkbox.getTag();

                    element.setActive(buttonView.isChecked());
                }

            }
            );



            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));

        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));

        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).showPermission());
        holder.checkbox.setChecked(list.get(position).isActive());

        if (list.get(position).isRedundant()) {
            holder.image.setImageDrawable(context.getResources().getDrawable(R.drawable.warning));
        } else {
            holder.image.setImageDrawable(context.getResources().getDrawable(R.drawable.ok));
        }
        return view;

    }

}

In the class extending ListActivity a defined ArrayAdapter is filled with an arrayList in an asyncTask (list informations are calculated in doInBackground() method):

public class PermissionViewActivity extends ListActivity {

    private final static String TAG = "PMA";
    ...
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.permissionview);
        ...
    }

    ... 

     private class CurrentPermissions extends AsyncTask<String, Integer, String> {

        private ListActivity la;
        private Context context;



        private int progress = 0;

        public CurrentPermissions(ListActivity la) {
            this.la = la;
            this.context = la;

        }

        protected void onPreExecute() {
        ...

        protected void onPostExecute(String result) {
          adapter = new InteractiveArrayAdapter(la, permissionList, changeable);
          la.setListAdapter(adapter);
          /output with number of listitems
              System.out.println("DEBUG1 " + la.getListAdapter().getCount());
        }
     }
      ...
      @Override
      protected void onListItemClick(ListView l,  View view, int position, long id) {
        super.onListItemClick(l, view, position, id);
        System.out.println("clicked !");
      }
      ...
    }

The onListItemClick method is not executed. When defining the onClickListener in the ArrayAdapter the click event works:

 view.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    System.out.println("Klick D1 " + list.get(viewHolder.pos));

                }

            });

Why does the definition of the click-event in the ArrayAdapter work, but overwriting in the ListActivity not ? I cant find the failure.

Upvotes: 2

Views: 5170

Answers (4)

swisscoder
swisscoder

Reputation: 129

If you have Clickable Views inside the ListItem, the focusable flag needs to be set to false. By setting the focusable=false in the layout, it will be overwritten when the Listener is set programatically.

My Solution worked for me by programmatically invoke set the focusability

listview.setOnItemClickListener(this);
listview.setFocusable(false);

Upvotes: 0

GHz
GHz

Reputation: 471

I do this in my view that contains the ListView by:

ListView lv = (ListView)findViewById(R.id.myList);
lv.setAdapter(new myCustomAdapter,items);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id){
        /* do something i've been clicked */

});

or here in an onItemSelected event

lv.setOnItemSelectedListener(new AdapterView.onItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id){
        ...
    }
    public void onNothingSelected(AdapterView<?> arg0){
        ...
    }
});

Hope this helps

Upvotes: 0

user
user

Reputation: 87064

Try to also set the CheckBox from the row layout as not focusable:

//...
 <CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px"
    android:focusable="false" 
    android:clickable="true">

//...

Upvotes: 4

dymmeh
dymmeh

Reputation: 22306

If you want to have a "general" click listener (applicable to all items in your listview) your activity should implement OnItemClickListener

public class PermissionViewActivity extends ListActivity implements OnItemClickListener

You will be required to override the method

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3)
{
     Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
}

Then you must manually set the click listener on your listview

ex.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.permissionview);
        ...
        getListView().setOnItemClickListener(this);
}

Upvotes: 0

Related Questions