问题描述
- Listview中第一个按钮无效,其他都正常
-
public View getView(final int position, View convertView, ViewGroup parent) {final ViewHolder holder; ListViewListener LvListener = null; if (convertView == null) { holder=new ViewHolder(); LvListener = new ListViewListener(position); convertView = mInflater.inflate(R.layout.location_list, null); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(LocationActivity.this, "您点击了:" + position, Toast.LENGTH_SHORT).show(); } }); holder.userId = (TextView) convertView.findViewById(R.id.userId); holder.img = (ImageView)convertView.findViewById(R.id.img); holder.info = (TextView)convertView.findViewById(R.id.info); holder.distance = (TextView)convertView.findViewById(R.id.distance); holder.btnAdd = (Button)convertView.findViewById(R.id.btnAdd); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } try { holder.userId.setText(mData.get(position).get("userId").toString()); holder.info.setText(mData.get(position).get("nickName").toString() + " " + mData.get(position).get("sex").toString()); holder.distance.setText((String)mData.get(position).get("distance") + "米"); holder.btnAdd.setOnClickListener(LvListener); holder.img.setImageResource(R.drawable.a1); } catch (Exception e) { e.printStackTrace(); } return convertView; }
解决方案
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(LocationActivity.this, "您点击了:" + position, //position这个position和你的点击不对应。如果listview往下滚动,第一个item点击的时弹出的position是滚动屏幕内的position
Toast.LENGTH_SHORT).show();
}
});
时间: 2024-12-10 15:46:31