我的Activity中有一个带有自定义适配器的ListView.
每个项目都有一个删除按钮.当有人点击该按钮时,会弹出一个对话框询问用户是否确实要删除该项目,点击是后,该项目将被删除.
我在内部验证了正确的项目被删除,但在视觉上只有列表中的最后一项被删除. 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(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。