动态 Add/Remove 根据数据值查看 CursorAdapter

Dynamically Add/Remove View CursorAdapter according with Data Value

我有一个 Cursor Adapter,我只想在字段值(从 cursor 访问)在 1 到 4 之间时显示一个按钮和一个 TextView,否则,这个视图将被删除。

所以,我用这个视图创建了一个布局文件,在 CursorAdapter 中,我检查从光标访问的字段是否在 1 到 4 之间,我删除视图,否则,我添加到布局:

    class Accounts_List_CursorAdapter extends CursorAdapter{
        //
        Context context;
        //

        public Accounts_List_CursorAdapter(Context context, Cursor c) {
            super(context, c, 0);
            this.context = context;

        }

        @Override
        //
        //Inflate layout of the rows
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return LayoutInflater.from(context).inflate(R.layout.row_list_accounts_data, parent, false);
        }

        @Override
        //
        //Set data and set changes to the row
        public void bindView(View view, final Context context, Cursor cursor) {
            //
            //Find the elements
            RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.layoutRowListAccountsData);
            TextView tvAccountsName = (TextView) view.findViewById(R.id.tvAccountsName);
            TextView tvAccountsInitValue = (TextView) view.findViewById(R.id.tvAccountsInitValue);
            TextView tvAccountsType = (TextView) view.findViewById(R.id.tvAccountsType);
            //
            //Get data from cursor
            final int accountId = cursor.getInt(cursor.getColumnIndexOrThrow(0));
            final String accountsName = cursor.getString(cursor.getColumnIndexOrThrow(1));
            final String accountsCurrValue = cursor.getString(cursor.getColumnIndexOrThrow(2));
            final String accountsInitValue = cursor.getString(cursor.getColumnIndexOrThrow(3));
            final String accountsType = cursor.getString(cursor.getColumnIndexOrThrow(4));
            // 
            tvAccountsName.setText(accountsName);
            tvAccountsCurrValue.setText("Current Value = " + accountsCurrValue);
            //    
           if ((accountId >= 1) && (accountId <= 4)){

                try {
                    relativeLayout.removeView(view.findViewById(R.id.cmdEditThisAccount));
                    relativeLayout.removeView(view.findViewById(R.id.tvAccountsInitValue));*
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else{
                //
relativeLayout.addView(view.findViewById(R.id.cmdEditThisAccount));
            relativeLayout.addView(view.findViewById(R.id.tvAccountsInitValue));
//
                tvAccountsInitValue.setText("Init Value = " + accountsInitValue);
                //
                Button cmdEditThisAccount = (Button) view.findViewById(R.id.cmdEditThisAccount);
                cmdEditThisAccount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    Intent intent = new Intent(context, UpdateAccount.class);
                       context.startActivity(intent);

                    }
                });
            }
        }
    }

问题是:当我 运行 这段代码时,会出现这样的信息: Java.lang.IllegalStateException: The specified child already has a parent.您必须先在 child 的 parent 上调用 removeView()。

我做错了什么,或者有另一种方法可以根据光标返回的数据动态隐藏和显示布局中的 TextView 和 Button?

提前致谢!

使用 View.setVisibility(FLAG)view.findViewById(R.id.cmdEditThisAccount).setVisibility(View.GONE)

api doc