我有一个活动,其中我有一个edittext
,我在其中输入内容并将该值存储在HashMap
中,其中edittext
中插入的数据是关键和静态数据是价值。现在我将它传递给DialogFragment,但我无法访问DialogFragment中的密钥。如何访问DialogFragment中的键以访问与该键关联的值
以下是代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="grepthorsoft.com.hash.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/tt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
HashMap<String, String> d;
EditText et;
String s;
FragmentManager fm;
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d = new HashMap<>();
et = findViewById(R.id.tt);
fm = getFragmentManager();
bt = findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
s = et.getText().toString();
d.put(s, "2");
MyDialog myDialog = new MyDialog(d);
myDialog.show(fm, "test");
}
});
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="grepthorsoft.com.hash.MyDialog">
</RelativeLayout>
public class MyDialog extends DialogFragment{
Button yes,no;
HashMap<String,String> d;
public MyDialog(){}
public MyDialog( HashMap<String,String> d)
{
this.d=d;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.activity_my_dialog,null);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
答案 0 :(得分:0)
您应该使用setArguments
这样的onclick函数传递值..
MyDialog myDialog = new MyDialog(d);
Bundle bundle = new Bundle();
bundle.putSerializable("hashmap",mMap);
myDialog.setArguments(bundle);
myDialog.show(fm, "test");
然后,您将使用yoyr getArguments
片段中的MyDialog
获取这些值
将此代码粘贴到onCreateView
Bundle bundle = this.getArguments();
if(bundle.getSerializable("hashmap") != null)
mMap = (HashMap<String, String>)bundle.getSerializable("hashmap");
//get your hashmap value
Iterator iterator = mMap.keySet().iterator();
while(iterator.hasNext()) {
String key=(String)iterator.next();
String value=(String)mMap.get(key);
}
hasNext()
如果迭代有更多元素,则返回true。
next()
返回迭代中的下一个元素。
答案 1 :(得分:0)
您可以在对话框片段 ...
中执行以下操作for ( String key : d.keySet() ) {
System.out.println("value" + d.get(key ));
}
中获取所有键
d.keySet()
从hashmapd
答案 2 :(得分:0)
您正在以错误的方式执行此操作..您应该通过DialogFragment.setArguments()方法将任何数据传递给DialogFragment。例如:
MainActivity类中的- &gt;
MyDialog myDialog = new MyDialog();
Bundle extras = new Bundle();
extras.putSerializable("HashMap",d);
myDialog.setArguments(extras);
myDialog.show(fm, "test");
在myDialog类中只保留默认构造函数并删除构造函数,然后将此代码添加到onActivityCreated方法
Bundle bundle = this.getArguments();
if(bundle != null) {
d = (HashMap<String, String>) bundle.getSerializable("HashMap");
}
有关详细信息,请查看here
答案 3 :(得分:0)
您可以通过在活动中使用hashmap public并在对话框片段类中访问对象((MainActivity)getActivity())来访问活动类中的hashmap声明.d;
在对话框片段中,您可以像这样迭代地图
void printMap() {
Iterator it = ((MainActivity)getActivity()).d.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}