我的主要活动中有 ListView(扩展活动),我正在尝试为列表视图中的项目实现 onItemClick。当用户单击列表视图条目时,将显示一个对话框,要求用户输入。使用下面的代码,当单击列表视图项目时,我没有从应用程序中得到任何响应。关于为什么会发生这种情况的任何想法?谢谢!
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;
}
}