user1107173
user1107173

Reputation: 10764

Java: Accessing fields in Downloaded Object

I'm new to Java, but know Objective-C. I need to access fields < keys, values > in a downloaded Object.

Below is the code:

car is an Schema and car_id is the field to query

Map<String, List<SMObject>> feedback = new HashMap<String, List<SMObject>>();
List<SMCondition> query = new ArrayList<SMCondition>();
DataService ds = serviceProvider.getDataService();
List<SMObject> results;

try {
            query.add(new SMEquals("car_id", new SMString(make)));
            results = ds.readObjects("car", query);

            if (results != null && results.size() > 0) {
                feedback.put(make, results);
            }

    }
....

results is an Object downloaded from a remote Database that is basically a HashMap. Assuming there is only one object that is returned each time, what would be the code to access Key & Values in the returned results object?

Complete Code in case you want to see it.

EDIT

Can I do something like this:

    SMObject resultObj;

     if (results != null && results.size() > 0) {
            resultObj = results[0];
            resultObj.put("resolved", "1");
            resultObj.put("accepted", "1");
            resultObj.put("declined", "0");

            String model = (String)resultObj.get("model");
        }

Upvotes: 0

Views: 72

Answers (1)

BlackHatSamurai
BlackHatSamurai

Reputation: 23493

If you wanted all the keys, you would do:

Map<String, List<SMObject>> feedback = new HashMap<String, List<SMObject>>();
List<String> myKeys = feedback.keySet();

To get the values, you would use the get method:

Map<String, List<SMObject>> feedback = new HashMap<String, List<SMObject>>();
feedback.get("yourKey"); 

For more info, check out: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

EDIT:

SMObject resultObj;

 if (results != null && results.size() > 0) {
        List<SMObject> myResults = feedback.get(make); 
        resultObj = myResults.get(0);
        resultObj.put("resolved", "1");
        resultObj.put("accepted", "1");
        resultObj.put("declined", "0");

        String model = (String)resultObj.get("model");
    }

The general concept is that you use the key, to get the value from the hashMap. That value happens to be a list of objects; therefore, you need to iterate over that list as well and retrieve each object from the list.

Upvotes: 1

Related Questions