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

时间:2014-07-08 13:31:20

标签: android listview android-listview

我的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", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int whichButton) {
                Sounds.buttonClick();


          } 
         })//setNegativeButton

         .create();

     myQuittingDialogBox.show();
 }    

}

2 个答案:

答案 0 :(得分:3)

更新代码: - 对象上的所有设置都需要在外面..检查更新的代码。

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);



   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, row_pos.getUsername());

           deleteFriendDialogBox(row_pos.getUsername(), position);             
       }
   });   

  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", new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int whichButton) {
                Sounds.buttonClick();


          } 
         })//setNegativeButton

         .create();

     myQuittingDialogBox.show();
 }    

}

答案 1 :(得分:0)

在调用notifyDatasetChange()

后再次加载适配器