android – 使用ListView,在适配器中删除总是删除最后一项

android – 使用ListView,在适配器中删除总是删除最后一项,第1张

概述我的Activity中有一个带有自定义适配器的ListView. 每个项目都有一个删除按钮.当有人点击该按钮时,会d出一个对话框询问用户是否确实要删除该项目,点击是后,该项目将被删除. 我在内部验证了正确的项目被删除,但在视觉上只有列表中的最后一项被删除. Activity.recreate显示正确的项目已被删除,但我不希望在简单的删除和notifyDataSetChanged()可以做到这一点时 我的Activity中有一个带有自定义适配器的ListVIEw.

每个项目都有一个删除按钮.当有人点击该按钮时,会d出一个对话框询问用户是否确实要删除该项目,点击是后,该项目将被删除.

我在内部验证了正确的项目被删除,但在视觉上只有列表中的最后一项被删除. Activity.recreate显示正确的项目已被删除,但我不希望在简单的删除和notifyDataSetChanged()可以做到这一点时调用recreate.

有人能指出我的代码可能出错的正确方向吗?

我活动中的源代码是:

ListVIEw myListvIEw = (ListVIEw) findVIEwByID(R.ID.ListFrIEnds);        SS4ListItemFrIEndsAdapter adapter = new SS4ListItemFrIEndsAdapter(getApplicationContext(),SS4NewGameActivity.this,SS4ListItemFrIEndsRows);          myListvIEw.setAdapter(adapter);

这是我的适配器:

public class SS4ListItemFrIEndsAdapter extends BaseAdapter { Context context; List<SS4ListItemFrIEndsRow> rowItems; private Activity activity;  SS4ListItemFrIEndsAdapter(Context context,Activity act,List<SS4ListItemFrIEndsRow> rowItems) {  this.context = context;  this.rowItems = rowItems;  this.activity = act; } @OverrIDe public int getCount() {  return rowItems.size(); } @OverrIDe public Object getItem(int position) {  return rowItems.get(position); } @OverrIDe public long getItemID(int position) {  return rowItems.indexOf(getItem(position)); } /* private vIEw holder class */ private class VIEwHolder {  ImageVIEw avatar;  ImageVIEw delete;  TextVIEw username; } @OverrIDe public VIEw getVIEw(final int position,VIEw convertVIEw,VIEwGroup parent) {  VIEwHolder holder = null;  LayoutInflater mInflater = (LayoutInflater) context    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);  if (convertVIEw == null) {   convertVIEw = mInflater.inflate(R.layout.ss4_List_item_frIEnds,null);   holder = new VIEwHolder();   holder.username = (TextVIEw) convertVIEw     .findVIEwByID(R.ID.username);   holder.avatar = (ImageVIEw) convertVIEw     .findVIEwByID(R.ID.avatar);   holder.delete = (ImageVIEw) convertVIEw             .findVIEwByID(R.ID.delete);   if (getCount() > 0) {              if (((SS4ListItemFrIEndsRow) getItem(0)).getDelete() == 0) {               holder.delete.setVisibility(ImageVIEw.INVISIBLE);       }   }   final SS4ListItemFrIEndsRow row_pos = rowItems.get(position);   holder.avatar.setimageResource(row_pos.getAvatar());   holder.username.setText(row_pos.getUsername());   holder.delete.setonClickListener(new VIEw.OnClickListener() {       public voID onClick(VIEw v) {           Sounds.buttonClick();           Log.d(App.TAG,row_pos.getUsername());           deleteFrIEndDialogBox(row_pos.getUsername(),position);                    }   });      convertVIEw.setTag(holder);  } else {   holder = (VIEwHolder) convertVIEw.getTag();  }  return convertVIEw; } private voID deleteFrIEndDialogBox(final String username,final int position) {    //String Title = "deleteFrIEnd?";    String msg = context.getResources().getString(R.string.ss4_delete_frIEnd);     AlertDialog myQuittingDialogBox =  new AlertDialog.Builder(activity)          .setCancelable(false)         .setTitle("")         .setMessage(msg)          .setPositivebutton("Ok",new DialogInterface.OnClickListener() {              public voID onClick(DialogInterface dialog,int whichbutton) {                     rowItems.remove(position);                     Log.d(App.TAG,"position: "+String.valueOf(position));                    notifyDataSetChanged();                                             SS4NewGameActivity.getListVIEwSize(myListvIEw);                                         //activity.recreate();                    try {                        int showMsgBox = (Integer) frIEnd.getInt("showMsgBox");                        if (showMsgBox == 1) {                            String msg = (String) frIEnd.getString("msg");                            Toast.makeText(context,msg,Toast.LENGTH_LONG).show();                        }                                       } catch (JsONException e) {                        // Todo auto-generated catch block                        e.printstacktrace();                    }                    Sounds.buttonClick();             }                       })//setPositivebutton         .setNegativebutton("Cancel",int whichbutton) {                Sounds.buttonClick();          }          })//setNegativebutton         .create();     myQuittingDialogBox.show(); }    }
解决方法 更新代码: – 对象上的所有设置都需要在外面..在这里检查更新的代码.

public class SS4ListItemFrIEndsAdapter extends BaseAdapter { Context context; List<SS4ListItemFrIEndsRow> rowItems; private Activity activity;  SS4ListItemFrIEndsAdapter(Context context,null);   holder = new VIEwHolder();   holder.username = (TextVIEw) convertVIEw     .findVIEwByID(R.ID.username);   holder.avatar = (ImageVIEw) convertVIEw     .findVIEwByID(R.ID.avatar);   holder.delete = (ImageVIEw) convertVIEw             .findVIEwByID(R.ID.delete);   convertVIEw.setTag(holder);  } else {   holder = (VIEwHolder) convertVIEw.getTag();  }   if (getCount() > 0) {              if (((SS4ListItemFrIEndsRow) getItem(0)).getDelete() == 0) {               holder.delete.setVisibility(ImageVIEw.INVISIBLE);       }   }   final SS4ListItemFrIEndsRow row_pos = rowItems.get(position);   holder.avatar.setimageResource(row_pos.getAvatar());   holder.username.setText(row_pos.getUsername());   holder.delete.setonClickListener(new VIEw.OnClickListener() {       public voID onClick(VIEw v) {           Sounds.buttonClick();           Log.d(App.TAG,position);                    }   });     return convertVIEw; } private voID deleteFrIEndDialogBox(final String username,int whichbutton) {                Sounds.buttonClick();          }          })//setNegativebutton         .create();     myQuittingDialogBox.show(); }    }
总结

以上是内存溢出为你收集整理的android – 使用ListView,在适配器中删除总是删除最后一项全部内容,希望文章能够帮你解决android – 使用ListView,在适配器中删除总是删除最后一项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127645.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存