Question

I very new using hashable in java, so i am setting value in one class in Java and getting value same class but could not getting another class, why? I don't know this?

My Data Actually, Like this:

  <rule code="53" value="QN_775_0=QN_775_0&gt;50" />
     <rule code="55" value="QN_775_2=QN_775_2&lt;3" />

I want to put key 53 and others and value QN_775_0>50. values is putting after logical operator.

Getting Data in same class like this:

 Logic lg= new Logic();

//like this value
 String logicCode="53";
 String  logicCode="QN_775_0&gt;50";

 lg.add(logicCode, logicCode);
 Enumeration<String> enumer = Logic.logicnew.keys();
 while (enumer.hasMoreElements()) {
 String keyFromTable = (String) enumer.nextElement();     
 System.out.println("1111111CODE="+keyFromTable + "VALUE = " + Logic.logicnew.get(keyFromTable));
     }  

My Model Class:

public class Logic {
    public static Hashtable<String,String> logicnew;

    public Logic(){
        logicnew=new Hashtable<String,String>();
    }

   public void add(String a, String b){
        logicnew.put(a,b);      
    }
     public String get(String key){
        return logicnew.get(key);
    }
     public int getSize(){
         return logicnew.size();
     }
     public Hashtable<String, String> getLogic(){
         return logicnew;
     }
     @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("\n");
        Set set = logicnew.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            sb.append("\t"+entry.getKey() + " : " + entry.getValue()+"\n");
        }
            return sb.toString();
    }
}

But in next activity , I am doing this but could not getting value.

Enumeration<String> enumer = Logic.logicnew.keys();
     while (enumer.hasMoreElements()) {
     String keyFromTable = (String) enumer.nextElement();     
     System.out.println("1111111CODE="+keyFromTable + "VALUE = " + Logic.logicnew.get(keyFromTable));
         }  

Here enumer.hasMoreElements() returning false value. why this???

Était-ce utile?

La solution

When your activity is killed, the static HashTable is re-initialized. Just add the log in your constructor:

 public Logic(){
    Log.d("App", "Inside Logic() Constructor ---");
    logicnew=new Hashtable<String,String>();
 }

If you see the log more than once, then it's clear indication that the HashTable is re-initialized. So your code enumer.hasMoreElements() returning false is the expected behavior since no elements are there in HashTable after it is re-initialized.

For clear idea on how to store and retrieve data in Android refer Data Storage in Android.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top