从适配器隐藏数据库行

时间:2017-01-30 22:43:42

标签: android listview android-sqlite

我有一个从Web服务调用填充的数据库。我想知道如何隐藏ListView中的一行?我不想删除记录只是隐藏它所以它不会在ListView中显示该行,因为我正在使用该行来填充上面的另一个ListView。有没有办法在employeeNumber这样的列中隐藏我的特定值?

我用光标填充ListView并希望有一种方法可以隐藏1行,因为我不希望它出现在我的ListView中。

public void displayBottomList() {
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        Cursor mBottomListCursor = db.rawQuery("SELECT * FROM employees", null);


        ListView mBottomListView = (ListView) findViewById(R.id.mDirectReportList);
        BottomListViewAdapter bottomAdapter = new BottomListViewAdapter(this, mBottomListCursor);
        mBottomListView.setAdapter(bottomAdapter);
    }

不确定如何使用我数据库中的Employee_number列隐藏此方法中的那一行。

public class BottomListViewAdapter extends CursorAdapter {
    int position;

    public BottomListViewAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        final int pos = cursor.getPosition();
        final ViewHolder viewHolder = (ViewHolder) view.getTag();

        String mFirstName = cursor.getString(cursor.getColumnIndexOrThrow("First_name"));
        String mLastName = cursor.getString(cursor.getColumnIndexOrThrow("Last_name"));
        String mTitle = cursor.getString(cursor.getColumnIndexOrThrow("Payroll_title"));
        String mThumbnail = cursor.getString(cursor.getColumnIndexOrThrow("ThumbnailData"));

        viewHolder.tvFirstName.setText(mFirstName);
        viewHolder.tvLastName.setText(mLastName);
        viewHolder.tvTitle.setText(mTitle);

        if(mThumbnail != null) {
            byte[] imageAsBytes = Base64.decode(mThumbnail.getBytes(), Base64.DEFAULT);
            Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
            viewHolder.mPeepPic.setImageBitmap(parsedImage);
        } else {
            viewHolder.mPeepPic.setImageResource(R.drawable.img_place_holder_adapter);
        }

        viewHolder.mDetailsButton.setTag(cursor.getPosition());
        viewHolder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.getTag();
                String mEmployeeNumber = cursor.getString(1);
                String mEmail = cursor.getString(8);
                String mFirstName = cursor.getString(2);
                String mLastName = cursor.getString(3);
                String mPhoneMobile = cursor.getString(4);
                String mPhoneOffice = cursor.getString(5);
                String mCostCenter = cursor.getString(10);
                String mHasDirectReports = cursor.getString(7);
                String mTitle = cursor.getString(6);
                String mPic = cursor.getString(9);
                Intent mIntent = new Intent(view.getContext(), EmployeeFullInfo.class);
                mIntent.putExtra("Employee_number", mEmployeeNumber);
                mIntent.putExtra("Email", mEmail);
                mIntent.putExtra("First_name", mFirstName);
                mIntent.putExtra("Last_name", mLastName);
                mIntent.putExtra("Phone_mobile", mPhoneMobile);
                mIntent.putExtra("Phone_office", mPhoneOffice);
                mIntent.putExtra("Cost_center_id", mCostCenter);
                mIntent.putExtra("Has_direct_reports", mHasDirectReports);
                mIntent.putExtra("Payroll_title", mTitle);
                mIntent.putExtra("ThumbnailData", mPic);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                view.getContext().startActivity(mIntent);
            }
        });
    }/*

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from()
        }
    }*/

    public static class ViewHolder {
        TextView tvFirstName;
        TextView tvLastName;
        TextView tvTitle;
        ImageView mPeepPic;
        ImageButton mDetailsButton;
        CardView mCardView;

        public ViewHolder(View view) {
            tvFirstName = (TextView) view.findViewById(R.id.personFirstName);
            tvLastName = (TextView) view.findViewById(R.id.personLastName);
            tvTitle = (TextView) view.findViewById(R.id.personTitle);
            mPeepPic = (ImageView) view.findViewById(R.id.person_photo);
            mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton);
        }
    }

    /*@NonNull
    @Override
    public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
        final Employee employee = getItem(position);
        final AppViewHolder holder;

        convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_cardview_layout, parent, false);
        holder = new AppViewHolder();

        holder.tvFirstName = (TextView) convertView.findViewById(R.id.personFirstName);
        holder.tvLastName = (TextView) convertView.findViewById(R.id.personLastName);
        holder.tvTitle = (TextView) convertView.findViewById(R.id.personTitle);
        holder.mPeepPic = (ImageView) convertView.findViewById(R.id.person_photo);
        holder.mDetailsButton = (ImageButton) convertView.findViewById(R.id.fullDetailButton);
        holder.mCardView = (CardView) convertView.findViewById(R.id.home_screen_cardView);

        if (employee != null) {
            holder.tvFirstName.setText(employee.getFirst_name());
            holder.tvLastName.setText(employee.getLast_name());
            holder.tvTitle.setText(employee.getPayroll_title());

            if (employee.getThumbnailData() != null) {
                String peepPicData = employee.getThumbnailData();
                byte[] imageAsBytes = Base64.decode(peepPicData.getBytes(), Base64.DEFAULT);
                Bitmap parsedImage = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
                holder.mPeepPic.setImageBitmap(parsedImage);
            }

            holder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String mEmployeeNumber = employee.getEmployee_number();
                    String mEmail = employee.getEmail();
                    String mFirstName = employee.getFirst_name();
                    String mLastName = employee.getLast_name();
                    String mPhoneMobile = employee.getPhone_mobile();
                    String mPhoneOffice = employee.getPhone_office();
                    String mCostCenter = employee.getCost_center_id();
                    String mHasDirectReports = employee.getHas_direct_reports();
                    String mTitle = employee.getPayroll_title();
                    String mPic = employee.getThumbnailData();
                    Intent mIntent = new Intent(view.getContext(), EmployeeFullInfo.class);
                    mIntent.putExtra("Employee_number", mEmployeeNumber);
                    mIntent.putExtra("Email", mEmail);
                    mIntent.putExtra("First_name", mFirstName);
                    mIntent.putExtra("Last_name", mLastName);
                    mIntent.putExtra("Phone_mobile", mPhoneMobile);
                    mIntent.putExtra("Phone_office", mPhoneOffice);
                    mIntent.putExtra("Cost_center_id", mCostCenter);
                    mIntent.putExtra("Has_direct_reports", mHasDirectReports);
                    mIntent.putExtra("Payroll_title", mTitle);
                    mIntent.putExtra("ThumbnailData", mPic);
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    view.getContext().startActivity(mIntent);
                }
            });

            holder.mCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String mEmployeeNumber = employee.getEmployee_number();
                    Intent mListBottomListIntent = new Intent(view.getContext(), MainActivity.class);
                    mListBottomListIntent.putExtra("Employee_number", mEmployeeNumber);
                    mListBottomListIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    view.getContext().startActivity(mListBottomListIntent);

                }
            });

            convertView.setTag(holder);
        }
        return convertView;
    }*/
}

public class CustomCursorWrapper extends CursorWrapper {

    private String mStartingEmployeeID = "3500075";
    private Employee employee;


    public CustomCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    @Override
    public int getCount() {

        //if you want to exclude just one row
        return super.getCount() - 1;
    }

    @Override
    public boolean moveToPosition(int position) {
        boolean b = super.moveToPosition(position);
        if(employee.getEmployee_number() != null && employee.getEmployee_number().contains(mStartingEmployeeID)) {
            //if this is the row you wanna skip
            return super.moveToPosition(position + 1);
        }

        return b;
    }
}

3 个答案:

答案 0 :(得分:1)

通常这是Tricky,因为你的光标会有那个条目。如果条目位于光标内的固定位置并且最好位于光标的头部或尾部,则会更容易一些。此外,当游标适配器调用“绑定视图”时,它会自动转到下一个条目。我不会建议直接摆弄光标。 如果您仍然想要这样做,请记住,您不仅必须覆盖bindview,还要覆盖适配器上的getCount方法。

编辑:或者,如果要隐藏的项目可以出现在Cursor中的动态位置,则使用Cursor包装器。基本上CursorWrapper包装Cursor并将所有方法调用委托给底层游标。但您可以自由地覆盖某些方法并插入业务逻辑。样本是 -

public class CustomCursorWrapper extends CursorWrapper {

    public CustomCursorWrapper(Cursor cursor) {
        super(cursor);
    }

    @Override
    public int getCount() {

        //if you want to exclude just one row
        return super.getCount() - 1;
    }

    @Override
    public boolean moveToPosition(int position) {
        boolean b = super.moveToPosition(position);
        if(false /*check whatever you have to check*/) {
            //if this is the row you wanna skip
            return super.moveToPosition(position + 1);
        }

        return b;
    }
}

API doc - https://developer.android.com/reference/android/database/CursorWrapper.html

请注意以上示例仅用于显示用法。您可能最终会覆盖更多的方法。试一试

编辑2 -

Cursor mBottomListCursor = db.rawQuery("SELECT * FROM employees", null);
CursorWrapper myWrapper = new CursorWrapper(mBottomListCursor );

ListView mBottomListView = (ListView) findViewById(R.id.mDirectReportList);
BottomListViewAdapter bottomAdapter = new BottomListViewAdapter(this, myWrapper );
mBottomListView.setAdapter(bottomAdapter);

答案 1 :(得分:0)

而不是SELECT * FROM employees执行SELECT <column1>,<column2>,<column3>,... FROM employees,列出除了您要隐藏的列之外的所有列。

无关:数据库访问不应与View交互在同一层中进行。

编辑:使用特定employeeNumber跳过一行可能是SELECT * FROM employees WHERE employeeNumber <> <number you're not looking for>

答案 2 :(得分:0)

如果您始终可以识别此行,只需在bindView中的BottomListViewAdapter添加支票即可。如果这是内置的默认适配器,只需对其进行扩展并编辑这一功能,您只需返回包含要隐藏的数据的行。