我是Android的新手,我不明白为什么当我从一个活动发送数据到另一个活动时,它不记得我以前保存的值,并且它无法向我显示购物车中的多个项目。
范例:
我有购物单和推车单
在购物列表中,我有一个recycleview
,当我按下添加按钮时,我可以将项目添加到购物车中,通过将数据发送到cart活动,将项目添加到购物车中,并且它显示recycle视图中的数据(此recycleview
位于Cart活动中)但现在如果我回去使用手机的后退按钮,我从购物清单中选择另一个项目,当我去购物车列表前一个项目添加它's不见了,我不明白为什么,因为我创建了一个ArrayList
,并将数据保存在其中。谁能告诉我我做错了什么?
我将添加3张照片来更好地解释自己。
的数据
的
在第二幅图中,如果我从购物车返回到购物列表并点击C,购物车中的A将消失,我将只有C。
下面是我的代码:
购物清单:
public class FirebaseSearch extends AppCompatActivity {
private EditText mSearchField;
private ImageButton mSearchBtn;
private ImageButton AddToCart;
private ImageButton Cart;
String searchText="";
private RecyclerView mResultList;
private DatabaseReference mUserDatabase;
private static ArrayList<Users> arrayList = new ArrayList<>();
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) {
cart_count++;
Context context = v.getContext();
Intent intent = new Intent(context, CartActivity.class);
intent.putExtra("Name",nome);
intent.putExtra("Surname",surname);
intent.putExtra("Prezzo",Long.toString(prezzo));
context.startActivity(intent);
}
});
}
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));
}
}
}enter code here
字符串
购物车:
public class CartActivity extends AppCompatActivity {
private RecyclerView cities;
private RecyclerView.Adapter adapter;
String nome,cognome;
Long prezzo;
int totalPrice=0;
TextView campoPrezzo;
int position=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
campoPrezzo=findViewById(R.id.TotalPrice);
//Receive the data from the previous activity
String passedArg = getIntent().getExtras().getString("Name");
nome=passedArg;
String passedArg2 = getIntent().getExtras().getString("Surname");
cognome=passedArg2;
Intent intent = getIntent();
String str = intent.getStringExtra("Prezzo");
ArrayList<Users> cities = initCities(nome,cognome,Long. parseLong(str));
this.cities = (RecyclerView) findViewById(R.id.cartList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
this.cities.setLayoutManager(mLayoutManager);
adapter = new ProductAdapter(cities);
this.cities.setAdapter(adapter);
System.out.println("Prezzo"+returnTotalPrice());
}
private ArrayList<Users> initCities(String nome,String cognome,Long price) {
ArrayList<Users> list = new ArrayList<>();
list.add(position,new Users(nome,cognome,price));
position++;
getTotalPrice(price);
return list;
}
public void getTotalPrice(Long price){
totalPrice+=price;
}
public int returnTotalPrice(){
return totalPrice;
}
}
型
1条答案
按热度按时间z3yyvxxp1#
您必须在数据库中保存您单击的元素(发送到购物车),以便它们不会再次加载或在每次进入购物车时显示。您只是在RecyclerView中填充所有选项,而不是保存ShoppingCartActivity中的选项。当您启动购物车Activity时,它将加载Intent中的附加项,如果您没有单击任何项目,则Intent不会有附加项,因此,如果您再次启动购物车,购物车将不会显示任何项目。如果您需要帮助设置数据库,请询问。你也应该添加firebase标签的职位。