Android 在SimpleAdapter的ListView中使用setTag和getTag时出现问题
我必须对listview中的项目执行多个操作,例如,一个按钮,这个按钮必须从jSON获取我在TextView上设置的文本,所以我尝试使用setTag和getTag,但我得到的代码值为Null。这是我截取的代码,提前谢谢 我的ExtendedsimpleAdapter视图扩展了Simpleadapter.javaAndroid 在SimpleAdapter的ListView中使用setTag和getTag时出现问题,android,listview,onclick,simpleadapter,Android,Listview,Onclick,Simpleadapter,我必须对listview中的项目执行多个操作,例如,一个按钮,这个按钮必须从jSON获取我在TextView上设置的文本,所以我尝试使用setTag和getTag,但我得到的代码值为Null。这是我截取的代码,提前谢谢 我的ExtendedsimpleAdapter视图扩展了Simpleadapter.java public class ExtendedSimpleAdapter extends SimpleAdapter{ ArrayList<HashMap<String, Obj
public class ExtendedSimpleAdapter extends SimpleAdapter{
ArrayList<HashMap<String, Object>> map;
String[] from;
int layout;
int[] to;
Context context;
LayoutInflater mInflater;
public ExtendedSimpleAdapter(Context context, ArrayList<HashMap<String, Object>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
layout = resource;
map = (ArrayList<HashMap<String, Object>>) data;
this.from = from;
this.to = to;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return this.createViewFromResource(position, convertView, parent, layout);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;
AtomPaymentHolder holder = null;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
} else {
v = convertView;
holder = new AtomPaymentHolder();
holder.share = (ImageButton)v.findViewById(R.id.share_item_session_button);
holder.share.setTag(position);
holder.ws_name = (TextView)v.findViewById(R.id.ws_name_item_session_text);
holder.ws_name.setTag(position);
// holder.img = (ImageView)v.findViewById(R.id.img_session);
// holder.img.setTag(holder.atomPayment);
v.setTag(holder);
}
this.bindView(position, v);
return v;
}
public static class AtomPaymentHolder {
ImageView img;
//TextView flip;
//TextView code;
TextView ws_name;
ImageButton share;
}
private void bindView(int position, View view) {
final HashMap<String, Object> dataSet = map.get(position);
if (dataSet == null) {
return;
}
final ViewBinder binder = super.getViewBinder();
final int count = to.length;
for (int i = 0; i < count; i++) {
final View v = view.findViewById(to[i]);
if (v != null) {
final Object data = dataSet.get(from[i]);
String text = data == null ? "" : data.toString();
if (text == null) {
text = "";
}
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, data, text);
}
if (!bound) {
if (v instanceof Checkable) {
if (data instanceof Boolean) {
((Checkable) v).setChecked((Boolean) data);
} else if (v instanceof TextView) {
// Note: keep the instanceof TextView check at the bottom of these
// ifs since a lot of views are TextViews (e.g. CheckBoxes).
setViewText((TextView) v, text);
} else {
throw new IllegalStateException(v.getClass().getName() +
" should be bound to a Boolean, not a " +
(data == null ? "<unknown type>" : data.getClass()));
}
} else if (v instanceof TextView) {
// Note: keep the instanceof TextView check at the bottom of these
// ifs since a lot of views are TextViews (e.g. CheckBoxes).
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
if (data instanceof Integer) {
setViewImage((ImageView) v, (Integer) data);
} else if (data instanceof Bitmap){
setViewImage((ImageView) v, (Bitmap)data);
} else {
setViewImage((ImageView) v, text);
}
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleAdapter");
}
}
}
}
}
private void setViewImage(ImageView v, Bitmap bmp){
v.setImageBitmap(bmp);
}
}
另一个例子是,当我点击共享按钮时,ImgView必须更改为另一个img,而这个URL img是在textView上设置的,所以我必须阅读这个textView,获取字符串并更改ImgView。所以我需要正确地设置标签和获取标签,这是我的问题。TY请发布活动和适配器的完整代码,无需单击即可完成位置适配器的所有代码,活动第一部分的代码只是对WS的请求,没有什么特别的-@DhairyOnClickHandler在布局上实现-android:onClick=shareOnClickHandler---@Amardeep
public void shareOnClickHandler(View v) {
AtomPaymentHolder CODE = (AtomPaymentHolder)v.getTag();
String codigo= CODE.ws_name.getText().toString();
}