Can we access Java.util Map of a class in another class
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) {开发者_如何学C
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.
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).
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)
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?
精彩评论