在我的项目中,我有一个列表视图,其中包含每个包含图像的列表项。现在,当单击特定列表项时,会打开与该项相关的新活动,并且它有 3 个按钮。根据单击的按钮,该特定列表项中的图像会发生变化。每个列表项都与数据库中的特定行相关联,我想知道如何识别特定的按钮单击并直接在列表项中更改图像视图。
我自定义Adapter的getView()方法如下
public class SpecialAdapter extends SimpleCursorAdapter{
private EventDBAdapter dbHelper;
@SuppressWarnings("deprecation")
public SpecialAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int i) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
private int[] colors = new int[] { 0xAAFFFFF, 0xAAc0c0c0 };
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
每个列表项都从此视图膨胀
<?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"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
>
<View
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_toLeftOf="@+id/tvListTitle"
android:paddingLeft="10dp"
android:background="#CC3232"/>
<TextView
android:id="@+id/tvListTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#108070"
android:text="Title in list"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvListTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00 to 00:00"
android:layout_below="@+id/tvListTitle"
android:textColor="#138F6A"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvListWhere"
android:layout_width="wrap_content"
android:layout_below="@+id/tvListTime"
android:layout_height="wrap_content"
android:text="Crystal plaza , Andheri (West) . "
android:textColor="#138F6A"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvListDetails"
android:layout_width="250dp"
android:layout_below="@+id/tvListWhere"
android:layout_height="20dp"
android:text="This is gonna contain brief summary about the event"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvListDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="14th January"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvListDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvListDetails"
android:text="Monday"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/ivStatus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvListWhere"
android:src="@drawable/yes" />
</RelativeLayout>