合并游标仅返回clickEvent中的第一个游标

时间:2017-02-22 17:51:35

标签: android listview android-cursor

点击位置的光标,但是当这样做时,日志显示我无论在哪里点击我都只获得第一行。

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));
                }
            }
        });

即使我在列表中单击20行,CursorAdapter onClickListener中的日志也只显示第一行游标。

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

在传递给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);
    }
}

在此处获取相同的光标以获取日志,但是没有获得光标对REFRESHLISTNUMBER日志语句的相同employee_number。这是列表中的随机数。

不确定为什么光标没有正确传递。我在每一行都有一个详细信息按钮,显示每行的光标信息并正确显示。

已完成的顶部适配器

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);
            }
        });
    }
}

也许在TopFragment中我的MergeCursor正在发生一些乱七八糟的事情?如果是这样,不确定如何解决它或为什么会发生。

1 个答案:

答案 0 :(得分:0)

通过向TopListCursorAdapter中的mTopCardView的setClickListener添加一行代码来修复。

cursor.moveToPosition(position);