• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Storing A hashmap in array and vice verse

I am Currently developing an application using firestore cloud as a back-end service. In addition, I want to store the hashmap values into an array and then passing it to the database. following code is what already achieved but still did not fit what I need.

Model Class

public class TripsInfoModel {
private String Country, Region, Balance, Stutas, updateDate, UserID;
List<String> TripsData;

public TripsInfoModel() {
}

public TripsInfoModel(String country, String region, String balance, String stutas, String updateDate, String userID,List<String> tripsData) {
    Country = country;
    Region = region;
    Balance = balance;
    Stutas = stutas;
    this.updateDate = updateDate;
    UserID = userID;
    TripsData = tripsData;


}



public String getCountry() {
    return Country;
}

public String getRegion() {
    return Region;
}

public String getBalance() {
    return Balance;
}

public String getStutas() {
    return Stutas;
}

public String getUpdateDate() {
    return updateDate;
}

public String getUserID() {
    return UserID;
}

public List<String> getTripsData() {
    return TripsData;
}

Send data to fire-store function

 String startDate = startPickDate.getText().toString().trim();
    String EndDate = endPickDate.getText().toString().trim();
    String Country = CountryName.getText().toString().trim();
    String CityNmae = mCity.getText().toString().trim();
    int Balance = Integer.parseInt(TripBalance.getText().toString());
    String Dsc = mDec.getText().toString().trim();

    HashMap <String, String>TripList = new HashMap<>();
    TripList.put("From",startDate);
    TripList.put("End",EndDate);
    TripList.put("Note",Dsc);

    String [] data = (String[]) TripList.values().toArray();
    List<String> TripsData = Arrays.asList(data);




    if (startDate.equals("") || EndDate.equals("")
            || Country.equals("") || CityNmae.equals("") || Balance <= 0 || Dsc.equals("")) {
        Toast.makeText(DateAndTime2.this, "Please identify  the date ,  or filed is missed", Toast.LENGTH_SHORT).show();

    } else {
        TripsInfoModel addData = new TripsInfoModel(Country,CityNmae,Balance,"Available",CurrentTime,UID,TripsData);
        db.collection("TripsInfo").document(UID).collection("SeprateData").document()
                .set(addData).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()){
                    Toast.makeText(DateAndTime2.this, "Data has added successfully",Toast.LENGTH_SHORT).show();
                }

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });

    }

In addition, the attched snaphot is showing what I want really to achive Triplist

      • 2
    • I can't tell from your description what exactly you expect the document to contain after your work is done. It looks to me you did exactly what you described - storing hashmap objects in an array.
      • 1
    • but it gives null values, can you please check the attached snapshot(TripsList Structure), how I can store the data like that. for example, the index [0] stored values which are from: Airport, note: Note1, to: Hotel. Hope u get it by now
      • 1
    • I don't understand your question. It looks like you haven't even written any code that actually adds the data to Firestore.

Once you receive the field data from the Cloud Firestore as a HashMap, map all the keys and values alternatively in an Array as shown below. All the keys are in even number locations and all the values are in odd number locations

   0       1       2       3      . . .   n-1      n         <--- Index

| Key1 | Value1 | Key2 | Value2 | . . . | Keyn | Valuen |    <--- Array

You can do this by using the HashMap.keySet() to get all the keys as a Set and then use the HashMap.get(key) to get the corresponding value. Feed it into the Array as shown and you are good to go.

Remember, Keyk will be at Array[k-1] and Valuek will be at Array[k]. For example, Key7 will be at Array[6] and its corresponding value Value7 will be at Array[7]

If you want a particular key and you do not know its index, just run a Linear Search on the Array to find the key and at the next index is its corresponding value.

To generate a HashMap from an Array, format the Array as shown above and use the HashMap.put(key, value)

The thing is, All HashMaps can be stored as an Array but storing an Array as a HashMap doesn't make sense at all times

EDIT: Forgot to answer the adding to Firestore thing

The tripList field shown in the image is an Array of HashMaps Just create an Array with Object datatype and load them with HashMaps as shown.

     0          1          2                  n
| HashMap0 | HashMap1 | HashMap2 | . . . | HashMapn |

Now, load this array into the Cloud Firestore through the following command

db.collection("TripsList").document(UID).update("tripList", HashMapArray);

Remember, there needs to be an array named tripList in Firestore to prevent getting errors

Hope this Helps..!

  • 1
Reply Report
      • 1
    • @Abdulmalek If my answer was able to solve your problem, please consider upvoting & marking it as the solution..!
      • 2
    • Your explanation is great but this thing can be performed during fetching the data . in my case I want to store the data as it shows in the attached image.
    • @Abdulmalek Please check out the updated Answer. I am not an expert in Firestore, I am more of a Realtime Database guy. So, I'm sorry if there are any minor errors in my answer. My main focus was to make you understand the process.