0

I am using a SortedMap in a calss that extends SimpleCursoradapter. can i acess that map from another class that extends ListActivity.

The code im using is given below.

public

class ListContacts extends ListActivity { 
ListAdapter 

lAdapter; 
@Override


public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.

activitylist); 

// 

/** 
* 

* Use the ContentResolver instance to query the database and return a

* Cursor with the contacts list. The query is performed against the URI

* stored in ContactsContract.Contacts.CONTENT_URI.

*/


Cursor cursor = getContentResolver().query(

ContactsContract.Contacts.

CONTENT_URI, null, 
ContactsContract.Contacts.

HAS_PHONE_NUMBER + " = 1", null,null); 
startManagingCursor(cursor);



// start mappings 

String[]  columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME }; 

int[] names  = new int[] { R.id.contact_name }; 


lAdapter = new ImageCursorAdapter(this, R.layout.main, cursor, columns,names); 



@Override 

protected void onListItemClick(ListView l, View v, int position, long id) { 

super.onListItemClick(l, v, position, id); 
}





} //  end of class ListContacts 



public

class ImageCursorAdapter extends SimpleCursorAdapter { 


private Cursor c; 

private Context context; 

SortedMap<String, String> 

phoneNumberMap = new TreeMap<String, String>(); 


public SortedMap<String, String> getPhoneNumberMap() { 

return phoneNumberMap; 
}



public void setPhoneNumberMap(SortedMap<String, String> phoneNumberMap) { 

this.phoneNumberMap = phoneNumberMap; 
}








public ImageCursorAdapter(Context context, int layout, Cursor c, 
String[] from, 

int[] to) { 

super(context, layout, c, from, to); 


this.c = c; 

this.context = context; 
}







public View getView(int pos, View inView, ViewGroup parent) { 
phoneNumberMap

.put("1", "fasfa"); 
  phoneNumberMap.put("2", "fasfa1"); 

phoneNumberMap.put("3", "fasfa2"); 

phoneNumberMap.put("4", "fasfa3"); 

phoneNumberMap.put("5", "fasfa4");

phoneNumberMap.put("6", "fasfa5");

System.

out.println(" Map : size: " + phoneNumberMap.size()); 

} 
}// end of  class ImageCursorAdapter

How can i access phoneNumberMap in the onListItemClick () method of Listcontacts class.

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
jennifer
  • 8,133
  • 22
  • 69
  • 96

3 Answers3

1

The way to do this is to create your own subclass of android.app.Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):

      class MyApp extends Application {   
      private String myState;   
      public String getState(){ 
         return myState;  
        }  
     public void setState(String s){    
       myState = s;  
       } 
      }  

     class Blah extends Activity {  

    @Override   

   public void onCreate(Bundle b){   

       ...     MyApp appState = ((MyApp)getApplicationContext()); 

          String state = appState.getState();   

            ...   } } 

This has essentially the same effect as using a static variable or singleton, but integrates quite well into the existing Android framework. Note that this will not work across processes (should your app be one of the rare ones that has multiple processes).

MorningGlory
  • 758
  • 1
  • 11
  • 21
0

As I recall SortedMap is mutable and you could do this

SortedMap<String, String> leakingSortedMap = lAdapter.getPhoneNumberMap();

If you will not change the values of the map, I suggest to modify getPhoneNumberMap like

public SortedMap<String, String> getPhoneNumberMap() { 

return Collections.unmodifiableMap(phoneNumberMap); 
}

Here it is explained - http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#unmodifiableMap(java.util.Map)

Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
  • First, I suggest you to read about immutability/mutabilty. Second, you must understand what memory leaking means. Third, are you sure that the place of lAdapter is in this exact activity - you can extract it to another class, make it Observable(or Singleton)? – Kiril Kirilov Jan 04 '11 at 09:53
  • By doing this you break the encapsulation! I suggest you to throw away the extreme programming and read some books first. – Kiril Kirilov Jan 04 '11 at 12:37
0

The more general problem you are encountering is how to save state across several Activities and all parts of your application. A static variable is a common Java way of achieving this.

private class ImageCursorAdapter extends SimpleCursorAdapter 
 {   
 ***private static SortedMap<String, String> phoneNumberMap = new TreeMap<String, String> ();***

 public static SortedMap<String, String> getPhoneNumberMap() {
    return phoneNumberMap;
}

  public static void setPhoneNumberMap(SortedMap<String, String> phoneNumberMap) {
    ImageCursorAdapter.phoneNumberMap = phoneNumberMap;
}

}

 public class class ListContacts extends ListActivity {

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

 System.out.println("Map size: : " + ImageCursorAdapter.getPhoneNumberMap().size());

Set s = ImageCursorAdapter.getPhoneNumberMap().entrySet();

Iterator it = s.iterator();

while (it.hasNext()) {
    Map.Entry m = (Map.Entry) it.next();

    String key = (String) m.getKey();

    String value = (String) m.getValue();

    System.out.println("loadContactDetails  Map : Value : Names : " + value);
    System.out.println("loadContactDetails  Map : Key : Names : " + key);
}

}

}

REFER: How to declare global variables in Android?

Community
  • 1
  • 1
jennifer
  • 8,133
  • 22
  • 69
  • 96