1

I have ListView in my main Activity(extends Activity) and I am trying to implement a onItemClick for the items in the listview. When the user clicks the listview entry a dialog will be shown asking for user input. With the below code I am not getting any response from the app when the listview items are clicked. Any thoughts on why this is happening? Thanks!

 list1 = (ListView) findViewById(R.id.lstPhotos);
            m_adapter = new PhotoAdapter(this, R.layout.listview_row, m_photos);
            list1.setAdapter(m_adapter);
        list1.setClickable(true);
        list1.setItemsCanFocus(false);
        list1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Log.v("listview", "listview item clicked");
                appErrorAlert("test", "test");
            }
        });

listview_row.xml

<?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:padding="6dip">

   <ImageView
        android:id="@+id/txtPhotoLocationActual"
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:focusable="false"
android:focusableInTouchMode="false"
   />


   <TextView
        android:id="@+id/txtPhotoCaptionActual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:textColor="#FFFFFF"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/txtPhotoLocationActual"
        android:gravity="center_vertical"
        android:focusable="false"
android:focusableInTouchMode="false"
   />

      <TextView
        android:id="@+id/txtLocation"
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:padding="3dip"
        android:textColor="#FFFFFF"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:focusable="false"
android:focusableInTouchMode="false"
   />

      <ImageButton android:id="@+id/delete"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:src="@drawable/delete_button"
        android:background="@null"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:focusable="false"
android:focusableInTouchMode="false" />

</RelativeLayout>

custom adapter where delete button click is called

public class PhotoAdapter extends ArrayAdapter<Photo> {

    private final ArrayList<Photo> objects;

    public PhotoAdapter(Context context, int textViewResourceId,
            ArrayList<Photo> objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Photo i = objects.get(position);
        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.listview_row, null);
            v.setClickable(true);
            v.setFocusable(true);
        }

        if (i != null) {
            ImageView thumb = (ImageView) v
                    .findViewById(R.id.txtPhotoLocationActual);
            TextView mtd = (TextView) v
                    .findViewById(R.id.txtPhotoCaptionActual);

            TextView loc = (TextView) v.findViewById(R.id.txtLocation);

            ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
            delete.setTag(position);

            if (thumb != null) {

                String imagePath = i.getLocation();
                loc.setText(imagePath);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                Bitmap bm = BitmapFactory.decodeFile(imagePath, options);
                final int REQUIRED_SIZE = 70;
                int scale = 1;
                while (options.outWidth / scale / 2 >= REQUIRED_SIZE
                        && options.outHeight / scale / 2 >= REQUIRED_SIZE)
                    scale *= 2;
                BitmapFactory.Options options2 = new BitmapFactory.Options();
                options2.inSampleSize = scale;
                Bitmap bm2 = BitmapFactory.decodeFile(imagePath, options2);
                thumb.setImageBitmap(bm2);
                delete.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("delete", "delete pressed");
                        objects.remove(i);
                        notifyDataSetChanged();

                    }
                });

            }

            if (mtd != null) {
                mtd.setText(i.getCaption());
            }
        }
        return v;

    }

}
adamwhiles
  • 115
  • 2
  • 13

4 Answers4

0

//////// Edited//////////////////

Remove this line list1.setClickable(true); and

Remove this line list1.setItemsCanFocus(false);

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

If u have any clickable items you may not get the list item clicked. try this it may be useful Android: ListView elements with multiple clickable buttons

Community
  • 1
  • 1
G_S
  • 7,068
  • 2
  • 21
  • 51
  • The imagebutton referred to in the xml file is clickable and that part works. I need to be able to click anywhere else(besides the delete button) to show the dialog box. – adamwhiles Aug 29 '12 at 03:25
  • Can i see the sample code where you implemented the button click – G_S Aug 29 '12 at 03:34
  • try checking this http://stackoverflow.com/questions/6184258/androidlistview-with-multiple-buttons-list-item-cant-be-clicked or check http://stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items – G_S Aug 29 '12 at 03:50
0

The problem is with multiple textview, if you check the documentation of ArrayAdapter the constructors take resourceId, and its Id for a layout that contains "a" textView.

jsDebugger
  • 122
  • 2
  • 11
0

Add this line to you parent layout of listview custom row xml file android:descendantFocusability="blocksDescendants"