RanjitRock
RanjitRock

Reputation: 1441

how to dynamically create hashmap key in java?

I need to implement and expandable list view where it extracts values from database and updates the Gui.so i need to store the values retrieved from cursor in hash map so i need dynamically add hash keys to hash-map and values. code :

for(int i=0;i<=1;i++){
         List resTimeArraySms=new ArrayList();
        resultCursorSMS = NTDBHelper.getInstance().getResultDataByMediaType(getApplicationContext(), "Plan1",1);

        while (resultCursorSMS.moveToNext()){

            HashMap resHash=new HashMap();
            resTimeStampSms = resultCursorSMS.getLong(resultCursorSMS.getColumnIndex(NTDataProvider.KEY_RESULTS_TIME_STAMP));
            resHash.put("timestampsms", resTimeStampSms);
            resTimeArraySms.add(resHash);

        } 



     resTimeArray.add(resTimeArraySms);
    }

Upvotes: 0

Views: 3945

Answers (2)

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

Try to create a counter and use the counter as the key and increment the counter value after each insertion

Example

int counter = 0;
            for(int i=0;i<=1;i++){
                List resTimeArraySms=new ArrayList();
               resultCursorSMS = NTDBHelper.getInstance().getResultDataByMediaType(getApplicationContext(), "Plan1",1);

               while (resultCursorSMS.moveToNext()){

                   HashMap resHash=new HashMap();
                   resTimeStampSms = resultCursorSMS.getLong(resultCursorSMS.getColumnIndex(NTDataProvider.KEY_RESULTS_TIME_STAMP));
                   resHash.put("timestampsms"+counter, resTimeStampSms);
                   resTimeArraySms.add(resHash);
                   counter++;
               } 



            resTimeArray.add(resTimeArraySms);
           }

Upvotes: 2

Scorpion
Scorpion

Reputation: 3976

In case you want to generate hashcode for the object retrieved from the DB, have a look at the HashCodeBuilder from commons-lang.

Upvotes: 0

Related Questions