将 onClick 从 CursorAdapter 传递到另一个 CursorAdapter

Pass onClick from CursorAdapter to another CursorAdapter

我有 2 个 ListView。一个在另一个之上。当用户单击 bottomList 中的一行时,我正在点击 Web 服务并创建一个新光标并重新填充 bottomList。同时,我想将点击的行放入 TopListView 中。我试图将一个 Intent 从 cursorAdapter 传递到 MainActivity,但这只是用与那里相同的值重新填充 bottomListView。甚至不调用网络服务。 TopListView 没有任何反应。

是否可以将onClick的位置从bottomCursorAdapter传递给topCursorAdapter?或者像 employee_number 这样的变量,这样我就可以在 TopAdapter 和 swapCursor 中查询它?如果是这样,不确定如何交换光标,因为无法判断是否有人单击了 topListView 中的 bottomListView。

我的 TopList 方法

public void displayTopList() {
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null);
        ListView mTopListView = (ListView) findViewById(R.id.mTopList);
        TopListCursorAdapter topAdapter = new TopListCursorAdapter(this, mTopCursor);
        mTopListView.setAdapter(topAdapter);
    }

我创建的从适配器获取数据以显示 toast 的方法

public void onRowClick(String mEmployeeNumber, String mFirstName) {
        Toast.makeText(getApplicationContext(), "SEND TO TOP LIST " + mEmployeeNumber + " " + mFirstName, Toast.LENGTH_LONG).show();
    }

我正在尝试添加 onRowClick 的 TopAdapter。

public class TopListCursorAdapter extends CursorAdapter {
    public TopListCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.contact_cardview_layout, 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.mCardView = (CardView) view.findViewById(R.id.home_screen_cardView);

        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(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(context, 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);
                v.getContext().startActivity(mIntent);
            }
        });
    }

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

如果我没理解错的话,您想让代码知道一个适配器中何时发生点击事件,然后将一些数据发送到另一个适配器。

在这种情况下,您可以简单地定义一个接口,通过包含两者的 Activity class 桥接适配器之间的 "communication"。

例如,这是您定义了接口的适配器。单击按钮时它会被激活。

public class TopListCursorAdapter extends CursorAdapter {

    public interface TopListClickListener {
        void onTopListClick(int position); // Can add more parameters here
    }

    private TopListClickListener callback;

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

    ...

        holder.mDetailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cursor.moveToPosition(position);
                ...
                if (callback != null) {
                    callback.onTopListClick(position); // Can pass more parameters here
                }

并且在 Activity 中,您可以实现该接口(也就是您的回调)

public class MainActivity extends AppCompatActivity 
    implements TopListCursorAdapter.TopListClickListener // see here

    // private ... topAdapter, bottomAdapter;

    @Override
    public void onTopListClick(int position) {
        // Make sure parameters match the interface ^^^


        // TODO: Get data from topAdapter
        // TODO: database update or arraylist.add to change bottomAdapter
        bottomAdapter.notifyDataSetChanged(); // Update the UI
    }

    // Other code can remain the same