ListView中Item有多个控件,其中控件的点击事件有问题
2个回答
展开全部
可以使用handler异步处理
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.icon_item, parent, false);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.item_text);
holder.icon = (ImageView) convertView.findViewById(R.id.item_icon);
holder.btn_del = (Button) convertView.findViewById(R.id.item_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(lists.get(position).get("sno"));
holder.icon.setImageResource(lists.get(position).get("image") == "0" ? R.drawable.flag_uk : R.drawable.flag_us);
final int location = position;
holder.btn_del.setFocusable(false);
holder.btn_del.setOnClickListener(new View.OnClickListener() {
// final String str=lists.get(location).get("sno");
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(mContext).setTitle("您确定删除吗?").setMessage(lists.get(location) + "\n"+lists.get(location).get("sno"))
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Message message = new Message();
message.what = 1;
message.arg1 = location;
mHandler.sendMessage(message);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).show();
}
});
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
Button btn_del;
}
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
lists.remove((int) msg.arg1);
adapter.notifyDataSetChanged();
break;
}
super.handleMessage(msg);
};
};
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询