全局变量扩展应用程序类 [英] Global variable extend application class
问题描述
所以,我想扩展基应用程序
类,并添加成员变量在以下链接这个第一个解决方案创造这样的全局变量。
这工作,如果成员变量就像一个字符串
或布尔
简单数据类型。但是,你会如何做一个更复杂的数据类型?在我来说,我想成员变量是类型的HashMap<字符串,布尔值>
我在 onActivityResult()
(一个布尔值,一个字符串
和<$设置三个成员变量C $ C>的HashMap&LT;字符串,布尔值&GT; ),我试图访问一个按钮的的的onClick()
方法,这些成员变量 onClickListener
。当我访问字符串和布尔变量的值设置适当。然而,当我访问的HashMap&LT;字符串,布尔值&GT;
变量的值设置为{},空的HashMap
。是否有某种连载
需要发生了复杂数据类型?
我还添加了一个的ArrayList&LT;布尔&GT;
作为成员变量,当我在访问这个变量的的onclick()
方法,它被设置正确。可能HashMaps这样必须设置不同。我不太确定是这一点。
在code以下,我只是显示一个精简版只包含了的HashMap
成员变量。
下面是应用程序
公共类MyApp的扩展应用{
私人的HashMap&LT;字符串,布尔值&GT; selectedContacts = NULL;
公众的HashMap&LT;字符串,布尔值&GT; getSelectedContacts(){
返回this.selectedContacts;
}
公共无效setSelectedContacts(HashMap的&LT;字符串,布尔值&GT; SC){
this.selectedContacts = SC;
}
}
下面是方法 onActivityResult()
和的onClick()
在我的信使活动
。在 onActivityResult()
,我设置的MyApp类的私有成员变量。在的onClick()
的方法,我称之为访问方法。
公共无效onActivityResult(INT REQ code,INT结果code,意图数据){
super.onActivityResult(REQ code,因此code,数据);
开关(REQ code){
案例R.integer.contact_manager:
如果(结果code == RESULT_CANCELED)
Log.d(的getString(R.string.debug_tag),失败);
如果(结果code == RESULT_OK){
Log.d(的getString(R.string.debug_tag),成功);
的MyApp appState =((MyApp的)getApplication());
appState.setSelectedContacts((HashMap的&LT;字符串,布尔值&GT;)data.getSerializableExtra(selectedContacts));
}
打破;
}
}
add_contact_button.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
意图I =新的意图(Messenger.this,ContactManager.class);
的MyApp appState =((MyApp的)getApplication());
HashMap的&LT;字符串,布尔值&GT; SC = appState.getSelectedContacts();
如果(SC!= NULL){
INT totalContacts = sc.size();
如果(totalContacts大于0){
束束=新包();
bundle.putSerializable(selectedContacts,SC);
i.putExtras(包);
}
}
startActivityForResult(ⅰ,R.integer.contact_manager);
}
});
所以,我解决了这个问题。在 onActivityResult()
的方法,我是通过遍历的HashMap
像这样:
迭代器&LT;进入&LT;字符串,布尔值&GT;&GT;它= sc.entrySet()迭代器()。
而(it.hasNext()){
HashMap.Entry双=(HashMap.Entry)it.next();
。字符串的ContactID = pairs.getKey()的toString();
布尔选定=(布尔)pairs.getValue();
如果(选择的){
selectedContactsText + =的getName(的ContactID)+,;
Log.d(信使的getName(的ContactID)+入选);
Log.d(信使,selectedContactsText +是文本);
}
it.remove(); //避免了ConcurrentModificationException的
}
不过,我注意到while循环的最后一条语句是从的HashMap
其实删除元素。所以,我评论这一行了,所有工程按预期。
So I am trying extend the base Application
class and add member variables to create global variables like in this first solution of the link below.
This works if the member variable is a simple data type like a String
or a Boolean
. But how would you do it for a more complex data type? In my case i would like the member variable to be of type HashMap<String, Boolean>
.
I am setting three member variables in onActivityResult()
(a boolean, a String
, and a HashMap<String, Boolean>
), and i am trying to access these member variables in the onClick()
method of a button's onClickListener
. When i access the string and boolean variables their values are set appropriately. However when i access the HashMap<String, Boolean>
variable its value is set to '{}', an empty HashMap
. Is there some kind of Serialization
that needs to happen with a complex data type?
I have also added an ArrayList<Boolean>
as a member variable and when i accessed this variable in the onclick()
method it was set correctly. Possibly HashMaps must be set differently. I'm not too sure are this point.
In the code below, I am just showing a stripped down version which only includes the HashMap
member variable.
Here is the my subclass of Application
public class MyApp extends Application {
private HashMap<String, Boolean> selectedContacts = null;
public HashMap<String, Boolean> getSelectedContacts() {
return this.selectedContacts;
}
public void setSelectedContacts(HashMap<String, Boolean> sc) {
this.selectedContacts = sc;
}
}
Below are the methods onActivityResult()
and onClick()
in my Messenger Activity
. In onActivityResult()
, I set the private member variable of the MyApp Class. In the onClick()
method, I call the accessor method.
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case R.integer.contact_manager:
if (resultCode == RESULT_CANCELED)
Log.d(getString(R.string.debug_tag), "FAILURE");
if (resultCode == RESULT_OK) {
Log.d(getString(R.string.debug_tag), "SUCCESS");
MyApp appState = ((MyApp)getApplication());
appState.setSelectedContacts((HashMap<String, Boolean>) data.getSerializableExtra("selectedContacts"));
}
break;
}
}
add_contact_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Messenger.this, ContactManager.class);
MyApp appState = ((MyApp)getApplication());
HashMap<String, Boolean> sc = appState.getSelectedContacts();
if (sc != null) {
int totalContacts = sc.size();
if(totalContacts > 0) {
Bundle bundle = new Bundle();
bundle.putSerializable("selectedContacts",sc);
i.putExtras(bundle);
}
}
startActivityForResult(i, R.integer.contact_manager);
}
});
So I fixed this issue. In the onActivityResult()
method, I was iterating through the HashMap
like so:
Iterator<Entry<String, Boolean>> it = sc.entrySet().iterator();
while (it.hasNext()) {
HashMap.Entry pairs = (HashMap.Entry) it.next();
String contactId = pairs.getKey().toString();
boolean selected = (Boolean) pairs.getValue();
if (selected) {
selectedContactsText += getName(contactId) + ", ";
Log.d("Messenger", getName(contactId) + " was selected");
Log.d("Messenger", selectedContactsText + " is the text");
}
it.remove(); // avoids a ConcurrentModificationException
}
However i noticed the last statement of the while loop was in fact removing elements from the HashMap
. So I commented this line out and all works as intended.
这篇关于全局变量扩展应用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!