我有一個ListView在我的活動與自定義適配器。使用ListView,刪除適配器總是刪除最後一個項目
每個項目都有一個刪除按鈕。當有人點擊按鈕時,會彈出一個對話框,詢問用戶是否真的想刪除該項目,點擊是,該項目被刪除。
我證實內部正確的項目被刪除,但從視覺上只有最後一個項目從列表中刪除。 Activity.recreate顯示正確的項目被刪除,但我不想在簡單的remove和notifyDataSetChanged()可以完成的時候調用重新創建。
有人能指出我正確的方向,可能是我的代碼有問題嗎?
在我的活動的源代碼是:
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();
}
}
嘗試在列表視圖中使用invalidateViews()和refreshDrawableState(),如果notifyDataSetChanged不工作只是加載適配器。 –
notifyDataSetChanged起作用,但刪除了錯誤的項目(始終是最後一項) –
剛剛嘗試invalidateViews()和refreshDrawableState()與notifyDataSetChanged一起使用,但仍然具有相同的效果。最後的項目每次都會被刪除 –