我正在尝试实现一个文本计数器,其中一个整数将在每个firestoreRecyclerView
项中递增和递减,例如0到100。我试过服用int[]
和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();
}
}
最佳答案
在这行代码中,holder.tvqty.setText(String.valueOf(a[position]++)),
是工作吗?尝试将[位置]的值存储为整数,然后将其设置为tvqty。
int current_value = a[position];
然后
holder.tvqty.setText(String.valueOf((current_value++)));