美文网首页
ListView的删除

ListView的删除

作者: 酸辣粉_2329 | 来源:发表于2016-12-08 00:42 被阅读0次

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

    相关文章

      网友评论

          本文标题:ListView的删除

          本文链接:https://www.haomeiwen.com/subject/snvjmttx.html