ListView在Android中的运用也是非常的频繁。
先记录一下如何给用户一个比较好的体验下删除ListView中的一项。
TODO: 代码修改
notifyDataSetChanged()
这个函数是关键
final List<String> list2 = new ArrayList<>();
list2.addAll(Arrays.asList(new String[]{"From A", "From B", "From C", "From D", "From E"}));
InvitationAdapter toMeAdapter = new InvitationAdapter(this, list2);
toMeList.setAdapter(toMeAdapter);
ArrayAdapter<String> toMeAdapter = new ArrayAdapter<String>(this, R.layout.friend_list_item, toMe);
toMeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long l) {
final ArrayAdapter<String> adapter = (ArrayAdapter<String>)parent.getAdapter();
AlertDialog.Builder builder = new AlertDialog.Builder(InvitationActivity.this);
builder.setTitle("Invitation");
builder.setMessage("Choose")
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
toMe.remove(position);
Log.d(TAG, "onClick: accept");
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d(TAG, "onClick: ");
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
网友评论