ListView onItemClick没有响应点击

时间:2012-08-29 03:06:54

标签: android listview onitemclicklistener

我在我的主Activity(扩展Activity)中有ListView,我正在尝试为listview中的项目实现onItemClick。当用户单击列表视图条目时,将显示一个对话框,询问用户输入。使用下面的代码,当点击listview项目时,我没有收到应用程序的任何响应。有关为什么会发生这种情况的任何想法?谢谢!

 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>

调用删除按钮的自定义适配器

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;

    }

}

4 个答案:

答案 0 :(得分:0)

////////编辑//////////////////

删除此行list1.setClickable(true);

删除此行list1.setItemsCanFocus(false);

答案 1 :(得分:0)

如果您有任何可点击的项目,则可能无法点击列表项目。 试试这可能有用Android: ListView elements with multiple clickable buttons

答案 2 :(得分:0)

问题在于多个textview,如果你检查ArrayAdapter的文档,构造函数需要resourceId,而它的Id是包含“a”textView的布局。

答案 3 :(得分:0)

将此行添加到listview自定义行xml文件的父布局 android:descendantFocusability =“blocksDescendants”