我们可以在另一个类中访问类的Java.util Map

时间:2011-01-04 09:10:54

标签: android

我在扩展SimpleCursoradapter的calss中使用SortedMap。我可以从另一个扩展ListActivity的类中获取该映射。

我正在使用的代码如下所示。

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

如何在Listcontacts类的onListItemClick()方法中访问phoneNumberMap。

3 个答案:

答案 0 :(得分:1)

执行此操作的方法是创建自己的android.app.Application子类,然后在清单中的application标记中指定该类。现在,Android将自动创建该类的实例,并使其可用于整个应用程序。您可以使用Context.getApplicationContext()方法从任何上下文访问它(Activity还提供了一个方法getApplication(),它具有完全相同的效果):

      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();   

            ...   } } 

这与使用静态变量或单例具有基本相同的效果,但与现有的Android框架完全集成。请注意,这不适用于整个流程(如果您的应用程序是少数具有多个流程的应用程序之一)。

答案 1 :(得分:0)

我记得SortedMap是可变的,你可以这样做

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

如果您不想更改地图的值,我建议修改getPhoneNumberMap,如

public SortedMap<String, String> getPhoneNumberMap() { 

return Collections.unmodifiableMap(phoneNumberMap); 
}

在此解释 - http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#unmodifiableMap(java.util.Map

答案 2 :(得分:0)

您遇到的更普遍的问题是如何跨多个活动和应用程序的所有部分保存状态。静态变量是实现此目的的常见Java方法。

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?