Firebase数据更改时,recyclerview倒挂

时间:2019-06-20 17:21:21

标签: android firebase firebase-realtime-database android-recyclerview recycler-adapter

我有一个从Firebase提取数据的recyclerview,如果Firebase中的数据在recyclerview中正常更改,则recyclerview内有编辑文本,但是如果我与编辑文本交互并且数据已更改,则recyclerview索引不稳定它颠倒了,这是一个Youtube链接,可以解释我的问题.... https://youtu.be/0AhLq2Cyeos .... 请注意,我在第二40点的视频中说过(如果我只单击而不插入数据),请更正为互动(单击或插入数据)

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    final String userid = user.getUid();
    usersDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(userid);
    recyclerView = view.findViewById(R.id.jardiList);

    AItem = new ArrayList<>();

    aAdapter = new jardiListAdapter(getContext(), AItem, this);
    aAdapter.setHasStableIds(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(aAdapter);
    fab = view.findViewById(R.id.fab_jardi);


    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int i = 0; i < AItem.size(); i++) {
                Adapterp mylist = AItem.get(i);
                View view1 = recyclerView.getChildAt(i);
                if (view1 != null) {
                    final TextView Qty = view1.findViewById(R.id.jSoldQty);
                    Map<String, Object> note6 = new HashMap<>();
                    note6.put(KEY_ITEMNAME, mylist.getItemName());
                    note6.put(KEY_ITEMOLD, mylist.getPurchasedQty());

                    int a =  Integer.valueOf(mylist.getPurchasedQty())-Integer.valueOf(mylist.getItemStill()) ;
                    Qty.setText(String.valueOf(a));
                    mylist.setItemStill(String.valueOf(a));
                    // note6.put(KEY_ITEMPRICE, mylist.getItemPrice());
                    if (mylist.getItemStill() == null) {
                        note6.put(KEY_ITEMSTILL, "0");
                        note6.put(KEY_ITEMSOLD, "0");
                        note6.put(KEY_SALESTOTAL, "0");
                    } else {

                        note6.put(KEY_ITEMSTILL, mylist.getItemStill());
                        note6.put(KEY_ITEMSOLD, mylist.getItemSold());
                        note6.put(KEY_SALESTOTAL, mylist.getSalesTotal());
                        //      currentTotal = currentTotal + Integer.valueOf(mylist.getSalesTotal());
                    }
                    note6.put(KEY_DATE, getDate());
                    //  note.put(KEY_TIME, mylist.getPurchasedQty());
                    //  note.put(KEY_P, mylist.getPurchasedQty());

                    usersDatabase.child("jardi").child(getDate()).child(p).child("Cigarette").child(mylist.getItemID()).updateChildren(note6);
                    usersDatabase.child("jardi1").child(p).child("Cigarette").child(mylist.getItemID()).updateChildren(note6);

                }

            }


        }
    });

编辑:我忘了说我只在横向模式下以人像模式面对问题,其正常工作而不会翻转

编辑:

 usersDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (p.equals("1")) {
                itemsDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(userid)
                        .child("inventory").child("p").child(p).child("Cigarette");
                itemsDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        AItem.clear();
                        for (DataSnapshot teacherSnapshot : dataSnapshot.getChildren()) {
                            Adapterp adapterp = teacherSnapshot.getValue(Adapterp.class);
                            adapterp.setKey(teacherSnapshot.getKey());
                            AItem.add(adapterp);
                        }
                        aAdapter.notifyDataSetChanged();


                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

编辑:这是jardiListAdapter.class

public class jardiListAdapter extends RecyclerView.Adapter<jardiListAdapter.RecyclerViewHolder> {

private Context mContext;
private List<Adapterp> item;
private OnItemClickListener mListener;

public jardiListAdapter(Context context, List<Adapterp> uploads, OnItemClickListener mListener1) {
    mContext = context;
    item = uploads;
    mListener = mListener1;
}

@Override
public jardiListAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.jardi_adapter, parent, false);
    return new jardiListAdapter.RecyclerViewHolder(v);
}

@Override
public void onBindViewHolder(final jardiListAdapter.RecyclerViewHolder holder, int position) {
    final Adapterp currentitem = item.get(position);
    holder.itemName.setText(currentitem.getItemName());
    holder.old.setText(currentitem.getPurchasedQty());
    holder.still.setText(currentitem.getItemStill());
    if(!holder.still.getText().toString().equals("")){
        int sum = Integer.valueOf(currentitem.getPurchasedQty())-Integer.valueOf(currentitem.getItemStill());
       holder.sold.setText(String.valueOf(sum));
    }
    if(currentitem.getSalesTotal()!=null){
        NumberFormat formatter = new DecimalFormat("#,###");
        double price = Double.valueOf(currentitem.getSalesTotal());
        String formattedNumber = formatter.format(price);
        holder.salestotal.setText(formattedNumber);
    }else {
        holder.salestotal.setText("0");
    }




}

@Override
public int getItemCount() {
    return item.size();
}

public void setOnItemClickListener(OnItemClickListener listener) {
    this.mListener = listener;
}

public interface OnItemClickListener {
    void onItemClick(int position, String newValue);

    void onShowItemClick(int position);

    void onDeleteItemClick(int position);
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
        , MenuItem.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener {
    public TextView itemName, old, sold, salestotal;
    public EditText still;
    public String cc;
    ImageButton optionBtn;

    public RecyclerViewHolder(View itemView) {
        super(itemView);

        itemName = itemView.findViewById(R.id.jitemname);
        old = itemView.findViewById(R.id.jOldQty);
        sold = itemView.findViewById(R.id.jSoldQty);
        salestotal = itemView.findViewById(R.id.jSalesTotal);
        still = itemView.findViewById(R.id.jStillQty);
        optionBtn = itemView.findViewById(R.id.jardi_option);
        optionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup(v);
            }
        });
        still.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mListener.onItemClick(getAdapterPosition(), still.getText().toString());

            }
        });


        itemView.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {

                switch (item.getItemId()) {
                    case R.id.smallJardi_btn:
                        mListener.onShowItemClick(position);
                        break;
                    case R.id.jardi_missingPurchases_btn:
                        mListener.onShowItemClick(position);
                        break;
                }
            }
        }
        return false;
    }

    public void showPopup(View view) {
        PopupMenu popup = new PopupMenu(mContext, view);
        try {
            // Reflection apis to enforce show icon
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                String POPUP_CONSTANT = "mPopup";
                if (field.getName().equals(POPUP_CONSTANT)) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popup);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                    String POPUP_FORCE_SHOW_ICON = "setForceShowIcon";
                    Method setForceIcons = classPopupHelper.getMethod(POPUP_FORCE_SHOW_ICON, boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        popup.getMenuInflater().inflate(R.menu.jardi_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(this);
        popup.show();
    }


}
}

这是我的布局

jardi_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:orientation="vertical"
android:visibility="visible"
app:cardBackgroundColor="#FFFFE8">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1.7">

    <TextView
        android:id="@+id/jitemname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:padding="8dp"
        android:textColor="#fa0" />

    <TextView
        android:id="@+id/jOldQty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:padding="8dp" />

    <EditText
        android:id="@+id/jStillQty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:inputType="number"
        android:padding="8dp" />

    <TextView
        android:id="@+id/jSoldQty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:padding="8dp"
        android:text="0" />

    <TextView
        android:id="@+id/jSalesTotal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:padding="8dp"
        android:text="0" />

    <TextView

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:padding="8dp"
        android:text="LBP" />

    <ImageButton
        android:id="@+id/jardi_option"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        android:background="#00000000"
        android:src="@drawable/arrow_24dp" />
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/black" />
</LinearLayout>

0 个答案:

没有答案