gpt4 book ai didi

java - 如何在 firestore recyclerview 中实现文本计数器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:21:33 25 4
gpt4 key购买 nike

我正在尝试实现文本计数器,其中一个整数将在每个 firestore RecyclerView 项目中递增和递减,例如 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]++)), 完成了 a[position]++ 的工作?尝试将 a[position] 的值存储在一个整数中,然后将其设置为 tvqty。

int current_value = a[position]; 

然后

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

关于java - 如何在 firestore recyclerview 中实现文本计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com