单击按钮后,recyclerview上下颠倒

时间:2019-06-13 10:25:47

标签: android firebase android-fragments android-recyclerview

我有一个Recyclerview,它从Firebase提取数据,当我单击fab按钮时,Recyclerview里面有编辑文本,其中一个for循环遍历Recyclerview内的编辑文本并将数据保存在Firebase中 我的问题是单击按钮时,recyclerview的一半已镜像

    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>

1 个答案:

答案 0 :(得分:1)

在回收商列表中使用编辑文本时出现问题。当您向下滚动时,即使您的用户尚未填写,回收者视图中的底部编辑文本也会被已填充的编辑文本值填充。

作为替代方法,您可以创建最适合您的任何数据结构稀疏数组,这些结构可以映射您的位置和值,例如mEditTextItem [] = new String [LIST_SIZE]。 ,假设您的列表项的位置与数组的索引匹配。

尝试使用文本监视程序mEditTextItem [POSITION] = YOUR_EDIT_TEXT_VALUE的值更新索引

要初始化编辑文本时,请使用mEditTextItem [POSITION]

的值。

通过此方法,您始终可以确保您的编辑文本具有正确的值。

在fab项目上单击,而不是遍历“回收者”视图,而是遍历mEditTextItem获得结果