删除列表行失败

时间:2014-07-03 09:54:15

标签: android listview adapter

我想从特定位置删除列表行,但它不起作用。

public void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );
    setContentView(R.layout.blocklist);
//  this.registerReceiver(receiver , new IntentFilter("com.pluggedin.DOWNLOADCOMPLETE"));
//  receivers.add(receiver);

    mDbHelper = new BlockDbAdapter(this);
    mDbHelper.open();

    listView = (ListView)findViewById(R.id.listView1);


//  facebookInvite =(ImageButton)findViewById(R.id.fbinvite);   // facebook invite code
    noContactsTextview =(TextView)findViewById(R.id.no_contacts_label);
    aController = (Controller) this.getApplicationContext();


listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position,
            long arg3) {
        // TODO Auto-generated method stub
        uName.clear();
        uMobile.clear();


        uName.add(adapterDataList.get(position).getContactName());   /*added by ritesh */
        uMobile.add(adapterDataList.get(position).getContactNumber());

        mDbHelper.createReminder(uName.get(0).toString(), uMobile.get(0).toString());

        TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        deviceIMEI = tManager.getDeviceId();
        v.showContextMenu();
    }
});

}
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    finish();
}
 @Override
    protected void onPause() {
        super.onPause();
        mDbHelper.close(); 
    }
@Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    cursor = this.getContentResolver().query(Uri.withAppendedPath(SyncContactsLooserProvider.CONTENT_URI,
            SyncContactsDatabase.Project.NAME), new String[] { BaseColumns._ID,
        SyncContactsDatabase.Project.C_NAME,
        SyncContactsDatabase.Project.C_CONTACT,
        SyncContactsDatabase.Project.C_SMALLIMAGE }, null, null, SyncContactsDatabase.Project.C_NAME+ " ASC");

    adapter = new MySimpleCursorAdapter(this, R.layout.contact_list_item, cursor,

            new String[] { SyncContactsDatabase.Project.C_NAME,
        SyncContactsDatabase.Project.C_CONTACT,
        SyncContactsDatabase.Project.C_SMALLIMAGE }, 

        new int[] {
        R.id.list_item_name, R.id.list_item_number, R.id.list_item_image });

    Cursor cursor = adapter.getCursor();
    int count = cursor.getCount();

    List blockedContactsList = GeneralUtil.getBlockedContactsList();
    List blockedContactsPositionList = new ArrayList<Intent>();
    if (count>0 && cursor.moveToFirst())
    {


        adapterDataList.clear();
        do{
            PhoneContacts contact = new PhoneContacts();
            contact.setContactName(cursor.getString(1));
            contact.setContactNumber(cursor.getString(2));
            if(blockedContactsList.contains(cursor.getString(2))){
                blockedContactsPositionList.add(new Integer(cursor.getPosition()));
            }
            adapterDataList.add(contact);
        }while(cursor.moveToNext());

        noContactsTextview.setVisibility(View.GONE);
    }
    else
    {

        noContactsTextview.setVisibility(View.VISIBLE);
    }


    listView.setAdapter(adapter);
    for (int i = 0; i < blockedContactsPositionList.size(); i++) {
        listView.removeViewAt(  ((Integer)(blockedContactsPositionList.get(i))).intValue()  );
        adapter.notifyDataSetChanged();
    }
}




class MySimpleCursorAdapter extends SimpleCursorAdapter {

    private SparseBooleanArray mSelectedItemsIds;

    public MySimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
        mSelectedItemsIds = new SparseBooleanArray();
        loader = new SyncContactsImageLoader(context, R.drawable.stub);

    }

    SyncContactsImageLoader loader = null;

    @Override
    public void setViewImage(ImageView v, String value) {
        v.setTag(value);
        loader.DisplayImage(value, (Activity)v.getContext(), v);
    }


    public void toggleSelection(int position) {
        selectView(position, !mSelectedItemsIds.get(position));
    }

    public void selectView(int position, boolean value) {
        if (value)
            mSelectedItemsIds.put(position, value);

        else
            mSelectedItemsIds.delete(position);
        notifyDataSetChanged();
    }

    public void removeSelection() {
        mSelectedItemsIds = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    public SparseBooleanArray getSelectedIds() {
        return mSelectedItemsIds;
    }
    public int getSelectedCount() {
        return mSelectedItemsIds.size();
    }
}

我想删除onclicklistener处的行。我每次都尝试listview.removeAt(position)但是它给了我异常适配器视图不支持。

1 个答案:

答案 0 :(得分:0)

试试这个:

adapterDataList.remove(adapterDataList.getItem(pos));
adapterDataList.notifyDataSetChanged();