使用简单光标适配器时如何在列表项中添加两个图像。我尝试通过扩展 SimpleCursorAdapter 类来覆盖 setViewImage。对于单个图像没有问题。但是如何添加两个图像?
MyFragment.java:
geoAdapter = new MySimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.listitem_cdf_geolist, c1, new String[] {DBConstants.LATITUDE,DBConstants.LONGITUDE,DBConstants.TOLERANCE,DBConstants.IMEI,DBConstants.IMEI}, new int[] {R.id.li_cdf_tv_latitude,R.id.li_cdf_tv_longitude,R.id.li_cdf_tv_tolerance,R.id.li_cdf_icon_georemove,R.id.li_cdf_icon_geoedit},0);
geoList.setAdapter(geoAdapter);
在 MySimpleCursorAdapter.java 文件中:
@Override
public void setViewImage(ImageView imageView, String imei) {
Log.i("simple", "text->"+imei);
//Here how to set two images and set imei as tag for those images
imageView.setTag(imei);
imageView.setImageResource(R.drawable.action_edit);
final ImageView myimageView=imageView;
imageView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.i("simple", "clicked");
String selectedIMEI = v.getTag().toString();
}
});
}
列表项布局 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="match_parent" >
<TextView
android:id="@+id/li_cdf_stv_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/li_cdf_stv_latitude" />
<TextView
android:id="@+id/li_cdf_tv_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/li_cdf_stv_latitude" />
<TextView
android:id="@+id/li_cdf_stv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/li_cdf_stv_latitude"
android:layout_below="@id/li_cdf_stv_latitude"
android:layout_marginTop="10dp"
android:text="@string/li_cdf_stv_longitude" />
<TextView
android:id="@+id/li_cdf_tv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/li_cdf_tv_latitude"
android:layout_below="@id/li_cdf_tv_latitude"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/li_cdf_stv_longitude" />
<TextView
android:id="@+id/li_cdf_stv_tolerance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/li_cdf_stv_longitude"
android:layout_below="@id/li_cdf_stv_longitude"
android:layout_marginTop="10dp"
android:text="@string/li_cdf_stv_tolerance" />
<TextView
android:id="@+id/li_cdf_tv_tolerance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/li_cdf_tv_longitude"
android:layout_below="@id/li_cdf_tv_longitude"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/li_cdf_stv_tolerance" />
<ImageView
android:id="@+id/li_cdf_icon_georemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="@string/li_cdf_icon_georemove"
android:onClick="removeGeofence" />
<ImageView
android:id="@+id/li_cdf_icon_geoedit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/li_cdf_icon_georemove"
android:contentDescription="@string/li_cdf_icon_geoedit"
android:onClick="editGeofence" />
<View
android:layout_width="1dp"
android:layout_height="30dp" >
</View>
</RelativeLayout>
更新 :
我试图超越 bindview。为每个列表项显示的图像。但是,如果我为每个图像设置标签,则将相同的标签应用于所有图像。还删除图标 onclick 事件仅在单击编辑和删除图像时触发。如何
@Override
public void bindView(View view, Context context, Cursor c) {
super.bindView(view, context, c);
ImageView geoEditIcon = (ImageView)view.findViewById(R.id.li_cdf_icon_geoedit);
geoEditIcon.setImageResource(R.drawable.action_editvehicle);
geoEditIcon.setTag(c.getString(c.getColumnIndex(DBConstants.IMEI)));
geoEditIcon.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.i("geolist", "geoRemoveIcon clicked");
String selectedIMEI = v.getTag().toString();
Log.i("geolist", "geoRemoveIcon selectedIMEI->"+selectedIMEI);
}
});
ImageView geoRemoveIcon = (ImageView)view.findViewById(R.id.li_cdf_icon_georemove);
geoRemoveIcon.setImageResource(R.drawable.action_removevehicle);
geoRemoveIcon.setTag(c.getString(c.getColumnIndex(DBConstants.IMEI)));
geoRemoveIcon.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.i("geolist", "geoRemoveIcon clicked");
String selectedIMEI = v.getTag().toString();
Log.i("geolist", "geoRemoveIcon selectedIMEI->"+selectedIMEI);
}
});
}