该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

如题,我的ListView的每个item都有一个删除按钮,但是每次点击按钮后都是删除了最后一个item。

调试后position是正确的,索引是没有问题的,每次删除的都是正确的位置,而且每次item一删除就调用notifyDataSetChanged()去刷新,这个问题困扰好几天了…麻烦大神帮帮忙。

public class MyAdapter extends BaseAdapter {

private Context context;

private LayoutInflater inflater;

public ArrayList arr;

public MyAdapter(Context context) {

super();

this.context = context;

inflater = LayoutInflater.from(context);

arr = new ArrayList();

}

@Override

public int getCount() {

return arr.size();

}

@Override

public Object getItem(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public long getItemId(int arg0) {

// TODO Auto-generated method stub

return arg0;

}

@Override

public View getView(final int position, View convertView, ViewGroup arg2) {

// TODO Auto-generated method stub

ViewHolder viewHolder = null;

if(convertView == null){

viewHolder = new ViewHolder();

convertView = inflater.inflate(R.layout.mission_item, null);

viewHolder.mission_name = (TextView) convertView.findViewById(R.id.pro_name);

viewHolder.mission_add = (TextView) convertView.findViewById(R.id.pro_add);

viewHolder.mission_phone = (TextView) convertView.findViewById(R.id.pro_phone);

viewHolder.del = (Button) convertView.findViewById(R.id.del);

SharedPreferences shared_mission = getSharedPreferences("mission",

Activity.MODE_PRIVATE);

viewHolder.mission_name.setText(shared_mission.getString("name", ""));

viewHolder.mission_add.setText(shared_mission.getString("address", ""));

viewHolder.mission_phone.setText(shared_mission.getString("phone", ""));

convertView.setTag(viewHolder);

}else

{

viewHolder = (ViewHolder) convertView.getTag();

}

viewHolder.del.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

arr.remove(position);

mission_adapter.notifyDataSetChanged();

}

});

listview.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView> arg0, View arg1, final int arg2,

long arg3) {

Intent intent = new Intent();

intent.setClass(MainActivity.this, MissionInfo.class);

startActivity(intent);

}

});

return convertView;

}

class ViewHolder

{

//任务界面

TextView mission_name,mission_add,mission_phone;

Button del;

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐