实现了一个listview,向左滑动会出现删除的按钮,下面是向左滑动后出现的效果:
这个时候我如果点击第一个删除设备的按钮,它会删除最后一个设备,继续点第一个删除按钮,会删除倒数第二个,总之就是删除最下面的一个,是什么原因引起的呢,望大神解答一下.
我的源码是这样的(因为调试,写的有点乱):
package com.example.realandriod; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class DeviceListActivity extends Activity { // 设备的信息应该从数据库中获取而来,此种写法不妥 private String[] devicename = { "设备1", "设备2", "设备3", "设备4" }; private int[] imagepath = { R.drawable.device1, R.drawable.device2, R.drawable.device3, R.drawable.device4 }; private List<Itemcontent> mData; // ArrayList<Itemcontent> mData = new ArrayList<Itemcontent>(); DraggableListView mListView = null; DeviceListActivity arrayList = null; MyListAdapter myAdapter = null; ArrayList<String> listString; public final class viewHolder1 { ImageView mimageView; TextView mtextView; Button mbutton; } public final class viewHolder2 { ImageView mimageView; } // private ArrayList<Object> array = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_device_list); // //这里的代码不够严谨,待改 // listString = new ArrayList<String>(); // for (int i = 0; i < 9; i++) { // listString.add(Integer.toString(i)); // } mData = new ArrayList<Itemcontent>(); for (int i = 0; i < devicename.length; i++) { mData.add(new Itemcontent(imagepath[i], devicename[i])); } mListView = (DraggableListView) findViewById(R.id.ListView); // myAdapter = new MyListAdapter(this, R.layout.activity_device_apt); myAdapter = new MyListAdapter(this, mData); mListView.setAdapter(myAdapter); } public class MyListAdapter extends BaseAdapter { int mTextViewResourceID = 0; private Context mContext; final int TYPE_1 = 0; final int TYPE_2 = 1; private LayoutInflater inflater; private float upX, downX; private List<Itemcontent> data; public MyListAdapter(Context context, List<Itemcontent> data) { this.mContext = context; //inflater = LayoutInflater.from(mContext); this.data = data; } public int getCount() { // System.out.println("当前的size是 "+data.size()); return this.data.size(); } public long getItemId(int position) { return position; } // 每加载一个listItem就调用一次getView() public View getView(final int position, View convertView, ViewGroup parent) { final View mView; int type = getItemViewType(position); // View row = convertView; final viewHolder1 holder1; final viewHolder2 holder2; if (convertView == null) { switch (type) { case TYPE_1: System.out.println("是正常条目,序号是 " + position); // mView = inflater // .inflate(R.layout.activity_device_apt, null); mView = LayoutInflater.from(this.mContext).inflate(R.layout.activity_device_apt, parent, false) ; // 查找布局文件Layout holder1 = new viewHolder1(); holder1.mimageView = (ImageView) mView .findViewById(R.id.image); holder1.mbutton = (Button) mView .findViewById(R.id.item_delete); holder1.mimageView.setImageResource(this.data.get(position) .getPath()); holder1.mtextView = (TextView) mView .findViewById(R.id.device); holder1.mtextView.setText(this.data.get(position).getName()); mView.setTag(holder1); holder1.mbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("移除的元素是 " + data.get(position).getName()); data.remove(position); notifyDataSetChanged(); } }); // 滑动显示删除按钮 mView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { downX = event.getX(); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { upX = event.getX(event.getPointerCount() - 1); if (downX - upX > 50) { mView.startAnimation(AnimationUtils .loadAnimation(mContext, R.anim.move)); holder1.mbutton.setVisibility(View.VISIBLE); } else if (upX - downX > 50) { mView.startAnimation(AnimationUtils .loadAnimation(mContext, R.anim.move2)); holder1.mbutton.setVisibility(View.GONE); } } return true; } }); break; //return mView; case TYPE_2: System.out.println("是添加条目,序号是 " + position); // mView = inflater.inflate(R.layout.activity_device_addslot, // parent, false); mView = LayoutInflater.from(this.mContext).inflate(R.layout.activity_device_addslot, parent, false); holder2 = new viewHolder2(); holder2.mimageView = (ImageView) mView .findViewById(R.id.device_addslot); holder2.mimageView.setImageResource(R.drawable.add_device); mView.setTag(holder2); //return mView; break; default: mView = LayoutInflater.from(this.mContext).inflate(R.layout.activity_device_apt, null); return mView; } }else{ System.out.println("调用了else语句"); return convertView; } return mView; } /* * @return TYPE_1:正常的item数据 TYPE_2:最后一个元素“+”号 */ public int getItemViewType(int position) { // TODO Auto-generated method stub if (position != 4) { return TYPE_1; } else { return TYPE_2; } } /* * @return 返回item的类型有多少种 */ public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return this.data.get(position); } } public void buttonClick(View v) { switch (v.getId()) { case R.id.device_addslot: startActivity(new Intent(DeviceListActivity.this, DeviceAddActivity.class)); break; } } }
上面之所以要有一个type,是因为我想在不同的type的时候加载不同类型的布局,但是现在我已经把TYPE_2的元素给去了,所以图中的布局都是一样的,现在case TYPE_2没有使用可以忽略。
我猜是不是集合的元素下标从0开始的,但是你传进去的position始终比下标大1啊,要不你再看看?
不是的,我每次删除的时候会执行
System.out.println("移除的元素是 " + data.get(position).getName());
打印出来的名字是对的,比如我按下第一个,打印的是设备1(上图名字依次是设备1.设备2.设备3,我改了好多,现在上面名字显示不出来了,不知道为什么,不过暂不纠结这个问题)是对的
@菜鸟的梦醒: 你删除后,调用notifyDataSetChanged,这里是不是去刷列表了,重新调用onCreate,此时devicename每次都少1,所以就加载总数-1个
遇到同样的问题,一下午都没有解决,断点跟好多遍,数据都是正常的,但是ListView更新时总是去掉最后一项。重新加载页面时,看到结果是正确的,就是删除后刷新显示的不对,欸,揪心滴。