Code example for PreferenceCategory

Methods: getPreferenceCount, removePreference

1
     * if no exclude list, show no-records info 
     */ 
    private void updateVisibility(){ 
        Log.e(TAG, "updateVisibility()");
        if(mExcludeListCategory!=null){
            int num = mExcludeListCategory.getPreferenceCount();
            Log.e(TAG, "exclude list size="+num);
            if(num>0){
                infoView.setVisibility(View.GONE);
                recordView.setVisibility(View.VISIBLE);
                return; 
            } 
        } 
        infoView.setVisibility(View.VISIBLE);
        recordView.setVisibility(View.GONE);
        return; 
    } 
     
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, OPTION_MENU_ID_ADD, 0, R.string.wifi_proxy_menu_add)
                .setIcon(R.drawable.ic_menu_scan_network); 
        menu.add(Menu.NONE, OPTION_MENU_ID_CLEAR, 0, R.string.wifi_proxy_menu_clear)
                .setIcon(android.R.drawable.ic_menu_manage);
//        menu.add(Menu.NONE, OPTION_MENU_ID_SAVE, 0, R.string.wifi_ip_settings_menu_save) 
//                .setIcon(android.R.drawable.ic_menu_save); 
        return super.onCreateOptionsMenu(menu);
    } 
     
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case OPTION_MENU_ID_ADD:
                addNewIp(); 
                return true; 
            case OPTION_MENU_ID_CLEAR:
                clearIp(); 
                return true; 
//            case OPTION_MENU_ID_SAVE: 
//                save(); 
//                return true; 
        } 
        return super.onOptionsItemSelected(item);
    } 
     
    @Override 
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {
        if (info instanceof AdapterContextMenuInfo) {
            Preference item = (Preference)getListView().getItemAtPosition(((AdapterContextMenuInfo) info).position);
            if(item instanceof ExcludeItem){
                mSelectedItem = (ExcludeItem)item;
                menu.setHeaderTitle(mSelectedItem.getTitle());
                menu.add(Menu.NONE, CONTEXT_MENU_ID_EDIT, 0, R.string.wifi_proxy_context_menu_edit);
                menu.add(Menu.NONE, CONTEXT_MENU_ID_DELET, 0, R.string.wifi_proxy_context_menu_delete);
            } 
        } 
    } 
     
    @Override 
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case CONTEXT_MENU_ID_EDIT:
                editIp(); 
                return true; 
            case CONTEXT_MENU_ID_DELET:
                deleteIp(); 
                return true; 
        } 
        return super.onContextItemSelected(item);
    } 
     
    @Override 
    public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference){
        if(preference instanceof ExcludeItem){
            mSelectedItem = (ExcludeItem)preference;
            editIp(); 
        } 
        return true; 
    } 
    public void onClick(DialogInterface dialog, int button) {
        if(button == BTN_OK){
            if(mDialog!=null && mExcludeListCategory!=null){
                String newIpString = mDialog.getIp();
                if(!mIsEdit && newIpString!=null && !"".equals(newIpString)){//add a new one
                    for(int i=0;i<mExcludeListCategory.getPreferenceCount();i++){
                        if(newIpString.equals(mExcludeListCategory.getPreference(i).getTitle())){
                            Log.i(TAG, "new added IP["+newIpString+"] already exist, ignore");
                            Toast.makeText(this, "new added exclude IP["+newIpString+"] already exist, ignore it", Toast.LENGTH_SHORT).show();
                            return; 
                        } 
                    } 
                    ExcludeItem item = new ExcludeItem(this, newIpString);
                    mExcludeListCategory.addPreference(item);
                    mIsNeedSave=true;
                }else{ 
                    if(mSelectedItem == null){
                        Log.e(TAG, "selected item is null, may be some error happened, have a check please");
                        return; 
                    } 
                    if(!mSelectedItem.getTitle().equals(newIpString)){
                        mIsNeedSave=true;
                        mSelectedItem.setTitle(newIpString);
                    } 
                } 
                save(); 
            } 
            updateVisibility(); 
        } 
    } 
     
    /** 
     * Edit a former input item 
     */ 
    private void editIp(){ 
        Log.i(TAG, "editIp()");
        mIsEdit = true;
        showDialog(mSelectedItem);
    } 
     
    /** 
     * delete the selected IP item 
     */ 
    private void deleteIp(){ 
        Log.i(TAG, "deleteIp()");
        if(mExcludeListCategory!=null && mSelectedItem != null){
            mExcludeListCategory.removePreference(mSelectedItem);
            mIsNeedSave=true;
            updateVisibility(); 
            save(); 
        } 
    }