如果多次单击某个选项,是否可以只获取 setOnItemClickListener 的单个值而不是获取多个值?
Is there a way for only getting a single value for a setOnItemClickListener instead of getting multiple values if an option is clicked multiple times?
我有一个清单,我想将清单中所有选中的项目发送到 Firebase 数据库。我已将它发送到数据库,但有一个问题。如果该项目被选中、取消选中并再次选中,该值将被发送到数据库两次。
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l)
{
String selectedItem = ((TextView) view).getText().toString();
selectedItems.add(selectedItem);
if (unselectedItems.contains(selectedItem)) {
unselectedItems.remove(selectedItem);
}
}
});
public void showSelectedItems(View view) {
FirebaseUser user = mAuth.getCurrentUser();
String presentStudents = "";
for (int i = 0; i < selectedItems.size(); i++) {
dbPresentRef.child(selectedItems.get(i)).push().setValue("Present");
presentStudents += "-" + selectedItems.get(i) + "\n";
}
}
只需在将 [=b12=] 添加到您的 selectedItems
列表之前添加一个 if
条件。
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l)
{
String selectedItem = ((TextView) view).getText().toString();
if(!selectedItems.contains(selectedItem)){
selectedItems.add(selectedItem);
}
if (unselectedItems.contains(selectedItem)) {
unselectedItems.remove(selectedItem);
}
}
});
public void showSelectedItems(View view) {
FirebaseUser user = mAuth.getCurrentUser();
String presentStudents = "";
for (int i = 0; i < selectedItems.size(); i++) {
dbPresentRef.child(selectedItems.get(i)).push().setValue("Present");
presentStudents += "-" + selectedItems.get(i) + "\n";
}
}
我有一个清单,我想将清单中所有选中的项目发送到 Firebase 数据库。我已将它发送到数据库,但有一个问题。如果该项目被选中、取消选中并再次选中,该值将被发送到数据库两次。
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l)
{
String selectedItem = ((TextView) view).getText().toString();
selectedItems.add(selectedItem);
if (unselectedItems.contains(selectedItem)) {
unselectedItems.remove(selectedItem);
}
}
});
public void showSelectedItems(View view) {
FirebaseUser user = mAuth.getCurrentUser();
String presentStudents = "";
for (int i = 0; i < selectedItems.size(); i++) {
dbPresentRef.child(selectedItems.get(i)).push().setValue("Present");
presentStudents += "-" + selectedItems.get(i) + "\n";
}
}
只需在将 [=b12=] 添加到您的 selectedItems
列表之前添加一个 if
条件。
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i,
long l)
{
String selectedItem = ((TextView) view).getText().toString();
if(!selectedItems.contains(selectedItem)){
selectedItems.add(selectedItem);
}
if (unselectedItems.contains(selectedItem)) {
unselectedItems.remove(selectedItem);
}
}
});
public void showSelectedItems(View view) {
FirebaseUser user = mAuth.getCurrentUser();
String presentStudents = "";
for (int i = 0; i < selectedItems.size(); i++) {
dbPresentRef.child(selectedItems.get(i)).push().setValue("Present");
presentStudents += "-" + selectedItems.get(i) + "\n";
}
}