如何删除使用简单游标适配器创建的列表项

时间:2014-04-22 08:49:17

标签: android simplecursoradapter contact-list

我想获取手机联系人列表并向他们发送邀请信息。我通过扩展简单的游标适配器来实现我的需求。现在我想从自定义适配器按下发送按钮后从列表中删除联系人(但不是从我的手机中删除)。请引导我走正确的路。

这是我到目前为止所做的:

活动类:

public class NotUsing extends Activity {

    ListView listContacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.not_using_kicka);
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.hide();

        listContacts = (ListView) findViewById(R.id.not_using_list);

        Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
        // Get All contacts
        CursorLoader cursorLoader = new CursorLoader(this, queryUri, null,
                null, null, sortOrder);

        Cursor cursor = cursorLoader.loadInBackground();

        String[] from = { ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
        int[] to = { R.id.phn_contact_name, R.id.phn_contact_img };

        ListAdapter adapter = new NotUsingAdapter(NotUsing.this,
                R.layout.phone_contacts_list, cursor, from, to,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listContacts.setAdapter(adapter);


    }

}

适配器类:

public class NotUsingAdapter extends SimpleCursorAdapter {

    String Name,PhotoThumbUri;
    int NameField, PhotoThumbField;

    public DisplayImageOptions options;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private ImageLoadingListener animateFirstListener;
    LibSettings dp;

    public NotUsingAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);

        Name = from[0];
        PhotoThumbUri = from[1];

        NameField = to[0];
        PhotoThumbField = to[1];

        dp = new LibSettings(context);
    }

    public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.phone_contacts_list, parent, false);
        return view;
    }

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        final String name = cursor.getString(cursor.getColumnIndex(Name));
        String photoUri = cursor.getString(cursor.getColumnIndex(PhotoThumbUri));

        TextView contactName = (TextView) view.findViewById(NameField);
        contactName.setText(name);

        ImageView contactImage = (ImageView) view.findViewById(PhotoThumbField);

        options = new DisplayImageOptions.Builder()
            .showImageForEmptyUri(R.drawable.picture_blank_square)
            .showImageOnFail(R.drawable.picture_blank_square)
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .considerExifParams(true)
            .displayer(new RoundedBitmapDisplayer(dp.convertPxtoDip(400)))
            .build();
        imageLoader.displayImage(photoUri, contactImage, options, animateFirstListener);

        ImageButton AddFrnd = (ImageButton) view.findViewById(R.id.add_frnd);

        AddFrnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View btnview) {
                Animation fadeOut = AnimationUtils.loadAnimation(context, R.anim.request_animate);
                fadeOut.setAnimationListener(new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        UserSession user = new UserSession(context);
                        String UserName = user.getLoginId();
                        //new SmsSend(context).execute("+61431977481",UserName);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Toast.makeText(context, name, Toast.LENGTH_LONG).show();
                        view.setVisibility(View.GONE);


                        //notifyDataSetChanged();
                    }
                });

                view.startAnimation(fadeOut);
            }
        });


    }
}

1 个答案:

答案 0 :(得分:0)

您应该从适配器中删除联系人项目,制作动画并调用notifyDatasetChanged()。

此操作不会触及您的手机通讯录。