addRow第一次正确添加到listView顶部,然后添加到列表底部

时间:2017-02-21 04:50:08

标签: android listview android-cursor

我从屏幕下半部分的列表视图中获取光标并将其添加到上半部列表视图中。这根本不是预期的100%。第一次单击底行时,它会将该行正确地添加到位置0的顶部列表视图中,并在其下方具有该行(应用程序打开时存在该行)。太棒了,这就是我想要的,除非我点击bottomList中的另一行。该行被添加到2位置,基本上不会变为0并推动其他2位。

现在,这对于我想要的东西是不正确的,并且相信这总是弄乱我所拥有的onClick事件,因为从被点击的topList传递的数据现在是错误的。不确定两者是否相关,所以试图至少让topList正确显示然后我可以调试更多,看看为什么传递的数据不正确。

这是我用来更新topList的代码,光标是从bottomList

传递的
    public class TopFragment extends Fragment {
    public Cursor mTopCursor;
    EmployeeDBHandler dbHandler;
    ListView mTopListView;
    public static MatrixCursor customCursor1;
    int flag = 0;
    TopListCursorAdapter mTopAdapter;

    @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";
        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"});
        dbHandler = EmployeeDBHandler.getInstance(getContext());
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        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);
        customCursor1.close();
        db.close();
        return view;

    }

    public void update(Cursor cursor) {
        if (cursor.moveToFirst()) {
            customCursor1.addRow(new Object[]{cursor.getInt(0), cursor.getString(cursor.getColumnIndex("Employee_number")), 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)});
            if (flag == 0) {
                customCursor1.addRow(new Object[]{mTopCursor.getInt(0), mTopCursor.getString(cursor.getColumnIndex("Employee_number")), mTopCursor.getString(2),
                        mTopCursor.getString(3), mTopCursor.getString(6), mTopCursor.getString(9),
                        mTopCursor.getString(8), mTopCursor.getString(4), mTopCursor.getString(5),
                        mTopCursor.getString(10), mTopCursor.getString(7), mTopCursor.getString(11)});
                flag++;
            } else {
                flag = 1;
            }
            mTopAdapter.changeCursor(customCursor1);
            customCursor1.close();
        }
    }
}

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);
                    Log.i("FROMCLICK", DatabaseUtils.dumpCursorToString(cursor));
                }
            }
        });
    }

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

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

2 个答案:

答案 0 :(得分:0)

每次更新光标时都不需要创建新的适配器。使用mTopAdapter作为您在onCreateView()中初始化的成员,然后使用以下方法更新update()方法中的光标:

mTopAdapter.changeCursor(customCursor1);
mtopAdapter.notifyDataSetChanged();

更新:始终将mTopCursor行保留为最后一行

基于this answer,您可以使用MergeCursorcustomCursor1mTopCursor合并为一个新的游标,供适配器使用:

public void update(Cursor cursor) {
    if (cursor.moveToFirst()) {
        customCursor1.addRow(new Object[]{cursor.getInt(0), cursor.getString(cursor.getColumnIndex("Employee_number")), 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[] {customCursor1, mTopCursor});
        mTopAdapter.swapCursor(newCursor);
        mtopAdapter.notifyDataSetChanged();
        customCursor1.close();
    }
}

答案 1 :(得分:0)

您是否尝试过适配器上的notifyDataSetChanged?

在我的自定义适配器中,它看起来像这样:

public void removeAllItems(MyDbHelper dbHelper)
    {
        //Go ahead and delete everything 
        ...
        notifyDataSetChanged();
    }

还有其他类似的变化选项并不是很大,如下所示:

    notifyItemInserted(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, mInjections.size());