gpt4 book ai didi

java - 每次单击按钮时 Firebase 都会创建一个新对象

转载 作者:行者123 更新时间:2023-12-02 08:59:02 26 4
gpt4 key购买 nike

我有一个问题/疑问。每次单击按钮时,我想将不同的对象保存到 firebase 实时数据库,但我遇到了问题,因为因为我有一个购物 list ,所以我单击更多项目,并且我想将它们保存到数据库,但它只保存最后一个项目我点击了,我想了解我做错了什么。我是 Android/Firebase 的新手,这是我第一次这样做,所以如果有人能告诉我我做错了什么,那就太棒了。太感谢了。我将附上代码和数据库的照片。

Activity :

public class FirebaseSearch extends AppCompatActivity {

private EditText mSearchField;
private ImageButton mSearchBtn;
private ImageButton AddToCart;
private ImageButton Cart;
int position=0;
String searchText="";

private RecyclerView mResultList;
private DatabaseReference mUserDatabase;



public static int cart_count = 0;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firebasesearch);




mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");

mSearchField = findViewById(R.id.search_field);
mSearchBtn = findViewById(R.id.search_btn);
mResultList = findViewById(R.id.result_list_cart);
AddToCart = findViewById(R.id.imageButton2);
Cart = findViewById(R.id.cartButton);

mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));

mSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchText = mSearchField.getText().toString();
firebaseUserSearch(searchText);
}
});

Cart.setOnClickListener(new View.OnClickListener() {
private Object Tag="Activity";

@Override
public void onClick(View v) {
if (cart_count < 1) {

} else {
startActivity(new Intent(FirebaseSearch.this, CartActivity.class));
}
}
});
}



private void firebaseUserSearch(String searchText) {
Toast.makeText(FirebaseSearch.this, "Started Search", Toast.LENGTH_LONG).show();
Query firebaseSearchQuery = mUserDatabase.orderByChild("Name").startAt(searchText).endAt(searchText + "\uf8ff");
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.list_layout,
UsersViewHolder.class,
firebaseSearchQuery
) {
@Override
protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {

viewHolder.getDetails(model.getName(), model.getSurname(),model.getPrice());
viewHolder.setDetails(model.getName(), model.getSurname(),model.getPrice());
}
};
mResultList.setAdapter(firebaseRecyclerAdapter);
}


@Override
protected void onStart() {
super.onStart();
invalidateOptionsMenu();
}

// View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
String nome;
String surname;
Long prezzo;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
ImageButton addToCart = (ImageButton)mView.findViewById(R.id.imageButton2);

addToCart.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
Users a = new Users(nome,surname,prezzo);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Cart");
myRef.setValue(a);
}
});
}

public void getDetails(String name,String cognome,Long price){
nome=name;
surname=cognome;
prezzo=price;
}


public void setDetails(String name, String surname, Long price) {

TextView user_name = (TextView) mView.findViewById(R.id.name_text);
TextView user_surname = (TextView)mView.findViewById(R.id.status_text);
TextView user_price = (TextView)mView.findViewById(R.id.price);

user_name.setText(name);
user_surname.setText(surname);
user_price.setText(Long.toString(price));

}
}




}

最佳答案

您需要使用push()方法:

myRef.push().setValue(a);

当前您添加的最后一个数据将覆盖之前的数据。 push() 方法将创建一个随机 ID 并分隔每个用户。

或者,如果您使用 Firebase 身份验证,则可以使用用户 uid 而不是 push() 来分隔您要保存的每个用户。

关于java - 每次单击按钮时 Firebase 都会创建一个新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60307479/

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