Merging cursor only gives back first cursor in clickEvent

11 Views Asked by At

Passing the cursor of position clicked but when doing so log is showing me I am getting only the first row no matter where I click.

holder.mTopCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCallback != null) {
                    mCallback.onTopListClick(cursor);
                    notifyDataSetChanged();
                    Log.i("FROMCLICK", DatabaseUtils.dumpCursorToString(cursor));
                }
            }
        });

The log inside the CursorAdapter onClickListener is showing only the first rows cursor even if I click 20 rows down the list.

@Override
    public void onTopListClick(Cursor cursor) {
        Log.i("FistClickBeforePassing", DatabaseUtils.dumpCursorToString(cursor));
        BottomFragment bottomFragment = (BottomFragment) getSupportFragmentManager().findFragmentById(R.id.bottomFragment);
        bottomFragment.refreshList(cursor);
    }

Same thing here before passing to the BottomFragment.

public void refreshList(Cursor cursor) {
        Log.i("RIGHTBEFOREREFRESH", DatabaseUtils.dumpCursorToString(cursor));
        String mEmployeeNumber = cursor.getString(1);
        Log.i("REFRESHLISTNUMBER", mEmployeeNumber);
        dbHandler = EmployeeDBHandler.getInstance(getContext());
        db = dbHandler.getReadableDatabase();
        mNewBottomCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " +
                /*"Employee_number" + "!=" + mStartingEmployeeID + " AND " +*/
                "Manager_employee_number" + "=" + mEmployeeNumber + " ORDER BY " +
                "Last_name" + " ASC", null);
        Log.i("THECURSOR ", DatabaseUtils.dumpCursorToString(mNewBottomCursor));
        BottomListCursorAdapter bottomListCursorAdapter = new BottomListCursorAdapter(getActivity(), cursor);
        bottomListCursorAdapter.swapCursor(mNewBottomCursor);
        mBottomListView.setAdapter(bottomListCursorAdapter);
    }
}

Get the same cursor here for the log but do not get the same employee_number the cursor has for the REFRESHLISTNUMBER log statement. That is a random number from the list.

Not sure why the cursor isnt being passed correctly. I have a details button on each row that displays info from the cursor for each row and that is displaying correctly.

Completed Top Adapter

public class TopListCursorAdapter extends CursorAdapter {

    public interface TopListClickListener {
        void onTopListClick(Cursor cursor);
    }

    private TopListClickListener mCallback;

    public TopListCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
        if(!(context instanceof TopListClickListener)) {
            throw new ClassCastException("Content must implement BottomListClickListener");
        }
        this.mCallback = (TopListClickListener) context;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.contact_cardview_top, parent, false);
    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {
        ViewHolder holder;
        holder = new ViewHolder();
        holder.tvFirstName = (TextView) view.findViewById(R.id.personFirstName);
        holder.tvLastName = (TextView) view.findViewById(R.id.personLastName);
        holder.tvTitle = (TextView) view.findViewById(R.id.personTitle);
        holder.mPeepPic = (ImageView) view.findViewById(R.id.person_photo);
        holder.mDetailsButton = (ImageButton) view.findViewById(R.id.fullDetailButton);
        holder.mTopCardView = (CardView) view.findViewById(R.id.mTopHomeScreenCV);

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

        holder.tvFirstName.setText(mFirstName);
        holder.tvLastName.setText(mLastName);
        holder.tvTitle.setText(mPayrollTitle);

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


        final int position = cursor.getPosition();
        holder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cursor.moveToPosition(position);
                String mEmployeeNumber = cursor.getString(cursor.getColumnIndex("Employee_number"));
                String mFirstName = cursor.getString(cursor.getColumnIndex("First_name"));
                String mLastName = cursor.getString(cursor.getColumnIndex("Last_name"));
                String mTitle = cursor.getString(cursor.getColumnIndex("Payroll_title"));
                String mPic = cursor.getString(cursor.getColumnIndex("ThumbnailData"));
                String mEmail = cursor.getString(cursor.getColumnIndex("Email"));
                String mPhoneMobile = cursor.getString(cursor.getColumnIndex("Phone_mobile"));
                String mPhoneOffice = cursor.getString(cursor.getColumnIndex("Phone_office"));
                String mCostCenter = cursor.getString(cursor.getColumnIndex("Cost_center_id"));
                String mHasDirectReports = cursor.getString(cursor.getColumnIndex("Has_direct_reports"));
                String mManagerNumber = cursor.getString(cursor.getColumnIndex("Manager_employee_number"));
                Intent mIntent = new Intent(context, EmployeeFullInfo.class);
                mIntent.putExtra("Employee_number", mEmployeeNumber);
                mIntent.putExtra("First_name", mFirstName);
                mIntent.putExtra("Last_name", mLastName);
                mIntent.putExtra("Payroll_title", mTitle);
                mIntent.putExtra("ThumbnailData", mPic);
                mIntent.putExtra("Email", mEmail);
                mIntent.putExtra("Phone_mobile", mPhoneMobile);
                mIntent.putExtra("Phone_office", mPhoneOffice);
                mIntent.putExtra("Cost_center_id", mCostCenter);
                mIntent.putExtra("Has_direct_reports", mHasDirectReports);
                mIntent.putExtra("Manager_employee_number", mManagerNumber);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                v.getContext().startActivity(mIntent);
            }
        });

        holder.mTopCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCallback != null) {
                    mCallback.onTopListClick(cursor);
                    notifyDataSetChanged();
                    Log.i("FROMCLICK", DatabaseUtils.dumpCursorToString(cursor));
                }
            }
        });
    }

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

public class TopFragment extends Fragment {
    Cursor mTopCursor;
    EmployeeDBHandler dbHandler;
    ListView mTopListView;
    TopListCursorAdapter mTopAdapter;
    MatrixCursor customCursor1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_top_list, container, false);
        String table = "employees";
        dbHandler = EmployeeDBHandler.getInstance(getContext());
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        customCursor1 = new MatrixCursor(new String[]{"_id", "Employee_number", "First_name",
                "Last_name", "Payroll_title", "ThumbnailData", "Email", "Phone_mobile", "Phone_office", "Cost_center_id",
                "Has_direct_reports", "Manager_employee_number"});
        int mStartingEmployeeID = mStartingNumber;
        mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null);
        mTopListView = (ListView) view.findViewById(R.id.mTopList);
        mTopAdapter = new TopListCursorAdapter(getContext(), mTopCursor);
        mTopListView.setAdapter(mTopAdapter);
        return view;

    }

    public void update(Cursor cursor) {
        if (cursor.moveToNext()) {
            customCursor1.addRow(new Object[]{cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(6), cursor.getString(9), cursor.getString(8), cursor.getString(4),
                    cursor.getString(5), cursor.getString(10), cursor.getString(7), cursor.getString(11)});

            MergeCursor newCursor = new MergeCursor(new Cursor[]{mTopCursor, customCursor1});
            mTopAdapter.swapCursor(newCursor);
            mTopAdapter.notifyDataSetChanged();
            scrollMyListToBottom();
            customCursor1.close();
        } cursor.moveToNext();
    }

    private void scrollMyListToBottom() {
        mTopListView.post(new Runnable() {
            @Override
            public void run() {
                mTopListView.setSelection(mTopAdapter.getCount() - 1);
            }
        });
    }
}

Maybe something is happening with my MergeCursor in TopFragment that is messing with the curors? If so, not sure how to fix it or why it would be happening.

1

There are 1 best solutions below

0
On

Fixed by adding a line of code to the setClickListener of the mTopCardView in the TopListCursorAdapter.

cursor.moveToPosition(position);