I have a ListView in my work with a custom adapter.
Each item has a delete button. When someone clicks on the button, a dialog box appears asking if the user really wants to delete the item by clicking "yes", the item will be deleted.
I checked that the internal elements are deleted, but only the last element from the list is visually deleted. Activity.recreate shows that the correct element has been removed, but I do not want to cause a re-creation when a simple remove and notifyDataSetChanged () can do the trick.
Can someone point me in the right direction as to what might be wrong with my code?
source code in my activity:
ListView mylistview = (ListView) findViewById(R.id.listFriends);
SS4ListItemFriendsAdapter adapter = new SS4ListItemFriendsAdapter(getApplicationContext(), SS4NewGameActivity.this, SS4ListItemFriendsRows);
mylistview.setAdapter(adapter);
This is my 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 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 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);
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) {
e.printStackTrace();
}
Sounds.buttonClick();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Sounds.buttonClick();
}
})
.create();
myQuittingDialogBox.show();
}
}