簡體   English   中英

在android中添加新數據時,ArrayList數據被清除

[英]ArrayList data gets cleared when adding new data in android

所以我的問題很簡單,但我還沒有找到解決辦法。 我有一個 ArrayList,用於存儲單擊按鈕的用戶的 uid(用戶 ID)。 然后將此數據發送到 firebase 的 Cloud Firestore。 當用戶單擊按鈕時,用戶 uid 將保存在 ArrayList 中。 但是,當我與其他用戶登錄並單擊按鈕時,第二個用戶的 uid 會覆蓋整個 ArrayList,而不是將第二個用戶的 uid 添加到 ArrayList,並且只有第二個用戶的 uid 會保存在 Cloud Firestore 上。

collectionReference = FirebaseFirestore.getInstance().collection("Fields");

collectionReference.document(venueID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (!task.getResult().exists()){

            //Here I declare the arrayList if the document doesn't exist
            peopleHereThisUpdates = new ArrayList<>();
            //The document that gets saved in Firestore, 
            // peopleHereThisUpdates is the main point in this question
            Map<String, Object> field = new HashMap<>();
            field.put(FIELD_NAME, venueName);
            field.put(PEOPLE_HERE_KEY, peopleHere);
            field.put(PEOPLE_HERE_BY_UID, peopleHereThisUpdates);
            collectionReference.document(venueID).set(field);

下一個代碼片段是我認為問題所在。 當我在按鈕按下方法中將 Arraylist 聲明為新的 Arraylist 時,每次按下按鈕時都會創建新的 arrayList。 我怎樣才能防止這種情況發生?

public void onButtonPressImHere() { //This gets called when button is pressed
    peopleHereThisUpdates = new ArrayList<>(); //This is the possible problem
    peopleHereThisUpdates.add(uid);

    collectionReference.document(venueID).update(PEOPLE_HERE_BY_UID, peopleHereThisUpdates);

整個活動代碼,

public class DetailFieldActivity extends Activity{

    public  final String PEOPLE_HERE_KEY = "People Here";
    public  final String FIELD_NAME = "Field Name";
    public  final String CURRENT_FIELD = "Current Field";
    public  final String UID = "Uid";
    public  final String PEOPLE_HERE_BY_UID = "People here by uid";
    ImageButton imTrainingHereButton;
    int peopleHere;
     //Get the size of this


    FirebaseFirestore db = FirebaseFirestore.getInstance();


    // The details of the venue that is being displayed.

    String venueName;
    private String venueID;
    private CollectionReference collectionReference;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    DocumentReference reference;
    String uid = user.getUid();
     ArrayList<String> peopleHereThisUpdates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_field_detail);
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        imTrainingHereButton = findViewById(R.id.imTrainingHereButton);



        Bundle venue = getIntent().getExtras();

        venueName = venue.getString("name");
        venueID = venue.getString("ID");

        setTitle(venueName);
        TextView fieldName = findViewById(R.id.fieldName);
        fieldName.setText(venueName);
        collectionReference = FirebaseFirestore.getInstance().collection("Fields");

        collectionReference.document(venueID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (!task.getResult().exists()){

                    //Here I declare the arrayList if the document doesn't exist
                    peopleHereThisUpdates = new ArrayList<>();
                    //The document that gets saved in Firestore,
                    // peopleHereThisUpdates is the main point in this question
                    Map<String, Object> field = new HashMap<>();
                    field.put(FIELD_NAME, venueName);
                    field.put(PEOPLE_HERE_KEY, peopleHere);
                    field.put(PEOPLE_HERE_BY_UID, peopleHereThisUpdates);
                    collectionReference.document(venueID).set(field);





                }
            }
        });



    }



    @Override
    protected void onStart() {
        super.onStart();
        imTrainingHereButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onButtonPressImHere();
            }
        });
    }






    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onButtonPressImHere() {


        reference = 
       FirebaseFirestore.getInstance().collection("Users").document(uid);

        Map<String, Object> users = new HashMap<>();
        users.put(CURRENT_FIELD, venueName);
        users.put(UID, uid);
        reference.set(users);




        peopleHereThisUpdates = new ArrayList<>();
        peopleHereThisUpdates.add(uid);

        collectionReference.document(venueID).update(PEOPLE_HERE_BY_UID, 
      peopleHereThisUpdates);
    }
}

是的,你是對的。 在這一行:

peopleHereThisUpdates = new ArrayList<>(); //This is the possible problem

每次觸發事件時都會創建一個新的ArrayList實例。 所以刪除它並初始化聲明中的peopleHereThisUpdates變量: ArrayList<String> peopleHereThisUpdates = new ArrayList<>(); 或者將它從onButtonPressImHere方法移到其他地方,比如在onCreate方法的onButtonPressImHere附近:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    peopleHereThisUpdates = new ArrayList<>();

只需為您的活動創建視圖模型並在那里聲明您的數組列表。 當活動被銷毀時,視圖模型中 arraylist 的值將保持不變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM