如何在Firestore RecyclerView中实现文本计数器

时间:2019-02-15 09:18:48

标签: java android android-recyclerview

我正在尝试实现文本计数器,其中每个Firestore RecyclerView项内的整数将递增和递减,例如0到100。我尝试使用int[]和{{1} } Button。我做了onClick。在holder.tvqty.setText(String.valueOf(a[position]++));上单击时,所有Button都会成功增加单个项目。但是主要的问题是,如果我单击第一个项目按钮,如果第一个位置相同,则实际上也是在增加最后一个位置,即总之,第一个TextViews和最后一个RecyclerView一起增加,而其余项目工作正常。

请帮助我调试此功能,将不胜感激。

这是我的java类:

public class OrderItemFragment extends Fragment{

    Context context;
    int i=0;
   // private List<Ex> exlist;
    RecyclerView recyclerView;
     FirebaseFirestore db;
     FirestoreRecyclerAdapter adapter;
    ArrayList<String> list = new ArrayList<String>();
    int [] a;


    public static OrderItemFragment newInstance() {
        OrderItemFragment fragment = new OrderItemFragment();
        return fragment;
    }
    public OrderItemFragment() {

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_orderitem, container, false);
        LinearLayoutManager layoutManager
                = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recorder);
        recyclerView.setLayoutManager(layoutManager);
        initializeData();
       //initializeAdapter();
        db=FirebaseFirestore.getInstance();

return view;
    }
    private void initializeData()
    {
        db=FirebaseFirestore.getInstance();
        Query query = db.collection("Items");


        FirestoreRecyclerOptions<FriendsResponse> response = new FirestoreRecyclerOptions.Builder<FriendsResponse>()
                .setQuery(query, FriendsResponse.class)
                .build();
        adapter = new FirestoreRecyclerAdapter<FriendsResponse, FriendsHolder>(response) {
            @Override
            public void onBindViewHolder(FriendsHolder holder, int position, FriendsResponse model) {
               /* for (int i=0;i < ids.size();i++)
                {
                    holder.exname.setText(ids.get(i));
                }*/
//Toast.makeText(getContext(),String.valueOf(getItemCount()),Toast.LENGTH_LONG).show();

                // String id = getSnapshots().getSnapshot(position).getId();
                String id = getSnapshots().getSnapshot(position).getId();
                for (i=0;i<getSnapshots().size();i++) {
                    list.add(getSnapshots().getSnapshot(position).getId());
                }
                a= new int [list.size()];
                holder.exname.setText(id);
holder.add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        holder.tvqty.setText(String.valueOf(a[position]++));

    }
});
holder.remove.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }
});

            }

            @Override
            public FriendsHolder onCreateViewHolder(ViewGroup group, int i) {
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.menu_item, group, false);

                return new FriendsHolder(view);
            }

            @Override
            public void onError(FirebaseFirestoreException e) {
                Log.e("error", e.getMessage());
            }
        };

        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);
    }
    public class FriendsHolder extends RecyclerView.ViewHolder {

        TextView exname,tvqty;
        ImageView add,remove;

        public FriendsHolder(View itemView) {
            super(itemView);
            exname= itemView.findViewById(R.id.menuname);
            add=itemView.findViewById(R.id.additem);
            remove=itemView.findViewById(R.id.removeitem);
            tvqty=itemView.findViewById(R.id.tvqty);

        }
    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }

/*
    public class Ex {
        String name;
        int logoId;
        int count=0;

        Ex(String name, int logoId) {
            this.name = name;
            this.logoId = logoId;
        }
    }

*/


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}

1 个答案:

答案 0 :(得分:0)

在这行代码holder.tvqty.setText(String.valueOf(a[position]++)),中,a[position]++会完成这项工作吗?尝试将 a [position] 的值存储在整数中,然后将其设置为tvqty。

int current_value = a[position]; 

然后

holder.tvqty.setText(String.valueOf((current_value++)));