将 url 或位图从 activity(父级)发送到片段(包含在 activity 中)
send a url or bitmap from activity(parent) to fragment(contained by this activity)
我需要帮助。我正在使用 Navigation Drawer(Material 设计)并将图片围成一个圆圈。现在这是在 Activity 中,当然还有片段。我希望这张图片位于主页片段中,这意味着当应用程序启动时图片必须出现,当我打开菜单时它也必须出现在圆圈中。问题是当我输入应用程序(用户名和密码)时,它向我发送了一个错误,当然是因为菜单中的 url 图像正在向片段发送空值。
现在我遵循下一个序列:OnCreate(activity) -> onStart(activity)->onCreateView(Fragment) ->onStart(fragment)。
在创建 Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
Log.d("Ejecutando", "Activity3");
}
onStart Activity:
@Override
protected void onStart() {
Log.d("Ejecutando", "onStartActivity()");
AlumnoSpinner = (Spinner)findViewById(R.id.spinner);
pref = PreferenceManager.getDefaultSharedPreferences(this);
String usuario = pref .getString("usuario", "null");
String password = pref .getString("password", "null");
params.put("username", usuario );
params.put("password", password);
ShowAlumnosSpinner(URL_alumnos, params);
.....
AlumnoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int i;
Set set = alumnosCopy.entrySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
for (i = 0; i < alumnosCopy.size(); i++) {
if (parent.getItemAtPosition(position).equals(entry.getValue().toString())) {
Toast.makeText(getApplicationContext(), "Selecciono" + entry.getValue().toString(), Toast.LENGTH_SHORT).show();
key = entry.getKey().toString();
editor = pref.edit();
editor.putString("alumnocodi", key);
editor.commit();
if (!nomCol.equals("")) {
imageUrl =//here i send the Url where i need the key, the object selected in the spinner
editor = pref.edit();
editor.putString("urlfotos", imageUrl + ".jpg");
editor.commit();
} else {
}
}
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
..............
}
onCreateView 片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Log.d("Ejecutando", "onCreateView()");
if (!isMyServiceRunning(BackgroundService.class)) {
alarmMgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent alarm = new Intent(getActivity(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarm, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
}
return rootView;
}
onStart 片段:
@Override
public void onStart() {
pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
String url = pref.getString("urlfotos", "null");
Log.d("Ejecutando", "onStart()");
new downloadImage().execute(url); // is a function that send the picture to an ImageView
super.onStart();
}
如果有人请帮我弄清楚如何将 URL 或位图发送到片段,因为那是我到目前为止所拥有的,它不起作用。谢谢:)
你主要有三种选择:
覆盖片段的 onAttach 方法,将接收到的上下文向下转换为 Activity,让片段询问 Activity 它需要什么。
使用getSupportFragmentManager通过id找到一个fragment,将其转换为对应的class,并通过您在Fragment中创建的setter传递信息。
以编程方式添加 Fragment(而不是将其添加到 XML 布局中)并使用您需要通过其 setArguments 方法传递的信息设置一个 Bundle。然后,您可以随时通过 getArguments.
在 Fragment 中检索此 Bundle
鉴于您当前的设置,我想第二个可能是一个不错的选择。
编辑
在您的 Activity 中,在 onCreate
方法中(或代码中任何可用 bitmap/url 的地方),您可以添加如下内容:
YourFragment fragment = (YourFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id);
fragment.setBitmap(bitmap);
fragment.setURL(url);
其中 setBitmap
和 setURL
是您在片段 class 上创建的方法。我假设您要在 XML 布局中添加您的片段。
我需要帮助。我正在使用 Navigation Drawer(Material 设计)并将图片围成一个圆圈。现在这是在 Activity 中,当然还有片段。我希望这张图片位于主页片段中,这意味着当应用程序启动时图片必须出现,当我打开菜单时它也必须出现在圆圈中。问题是当我输入应用程序(用户名和密码)时,它向我发送了一个错误,当然是因为菜单中的 url 图像正在向片段发送空值。 现在我遵循下一个序列:OnCreate(activity) -> onStart(activity)->onCreateView(Fragment) ->onStart(fragment)。 在创建 Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
Log.d("Ejecutando", "Activity3");
}
onStart Activity:
@Override
protected void onStart() {
Log.d("Ejecutando", "onStartActivity()");
AlumnoSpinner = (Spinner)findViewById(R.id.spinner);
pref = PreferenceManager.getDefaultSharedPreferences(this);
String usuario = pref .getString("usuario", "null");
String password = pref .getString("password", "null");
params.put("username", usuario );
params.put("password", password);
ShowAlumnosSpinner(URL_alumnos, params);
.....
AlumnoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int i;
Set set = alumnosCopy.entrySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
for (i = 0; i < alumnosCopy.size(); i++) {
if (parent.getItemAtPosition(position).equals(entry.getValue().toString())) {
Toast.makeText(getApplicationContext(), "Selecciono" + entry.getValue().toString(), Toast.LENGTH_SHORT).show();
key = entry.getKey().toString();
editor = pref.edit();
editor.putString("alumnocodi", key);
editor.commit();
if (!nomCol.equals("")) {
imageUrl =//here i send the Url where i need the key, the object selected in the spinner
editor = pref.edit();
editor.putString("urlfotos", imageUrl + ".jpg");
editor.commit();
} else {
}
}
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
..............
}
onCreateView 片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Log.d("Ejecutando", "onCreateView()");
if (!isMyServiceRunning(BackgroundService.class)) {
alarmMgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent alarm = new Intent(getActivity(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarm, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 6);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
}
return rootView;
}
onStart 片段:
@Override
public void onStart() {
pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
String url = pref.getString("urlfotos", "null");
Log.d("Ejecutando", "onStart()");
new downloadImage().execute(url); // is a function that send the picture to an ImageView
super.onStart();
}
如果有人请帮我弄清楚如何将 URL 或位图发送到片段,因为那是我到目前为止所拥有的,它不起作用。谢谢:)
你主要有三种选择:
覆盖片段的 onAttach 方法,将接收到的上下文向下转换为 Activity,让片段询问 Activity 它需要什么。
使用getSupportFragmentManager通过id找到一个fragment,将其转换为对应的class,并通过您在Fragment中创建的setter传递信息。
以编程方式添加 Fragment(而不是将其添加到 XML 布局中)并使用您需要通过其 setArguments 方法传递的信息设置一个 Bundle。然后,您可以随时通过 getArguments.
在 Fragment 中检索此 Bundle
鉴于您当前的设置,我想第二个可能是一个不错的选择。
编辑
在您的 Activity 中,在 onCreate
方法中(或代码中任何可用 bitmap/url 的地方),您可以添加如下内容:
YourFragment fragment = (YourFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id);
fragment.setBitmap(bitmap);
fragment.setURL(url);
其中 setBitmap
和 setURL
是您在片段 class 上创建的方法。我假设您要在 XML 布局中添加您的片段。