获取一个字符串到另一个 activity
Getting a String to another activity
(我想制作一个应用程序来显示新闻网站的 40 篇文章。)我在将字符串获取到另一个 class 时遇到问题。我的 "Menu1.java" 中有一个字符串 "link",我想在 "Menu1_1_Artikel.java" 中使用它。
Getter 方法有效。但我不知道如何设置 "link" 的值。每次我尝试它时,它只会得到 "null" 而不是在列表视图上单击的文章的 link 。 :/
public class Menu1 extends Fragment {
private ListView lv; //Listview which shows 40 articles from a news website
private ArrayAdapter adapter;
public ArrayList liste = new ArrayList();
private ProgressDialog progressDialog;
private String[] myStringArray = new String[40]; //Array with 40 URLs
public String link;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu_1, container, false);
lv = (ListView)view.findViewById(R.id.list);
adapter = new ArrayAdapter(getActivity(), R.layout.list_item, liste);
new datenAbrufen().execute(); //Method which loads the 40 articles
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
link = myStringArray[position]; //Save the URL to the article which is clicked on the listview to "link"
Menu1 obj = new Menu1();
obj.setLink(link); //I think here is the problem because the value is not in the Getter Method..
Toast.makeText(getActivity(), link, Toast.LENGTH_LONG).show(); //Output: Shows the URL from article -> "link" have a value
//Fragment
Fragment fragment = null;
fragment = new Menu1_1_Artikel();
if (fragment != null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
}
});
return view;
}
public String getLink(){
return this.link; //Problem: String "link" = null
}
public void setLink(String Str){
this.link= Str;
//Toast.makeText(getActivity(), link, Toast.LENGTH_LONG).show(); //Toast would show an Error: "Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference"
}
//[......]
}
public class Menu1_1_Artikel extends Fragment {
private String link;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container, false);
link= "Test";
Toast.makeText(getActivity(), "1.: "+link, Toast.LENGTH_LONG).show(); //Output: "Test"
Menu1 obj = new Menu1();
link= obj.getLink();
Toast.makeText(getActivity(), "2.: "+link, Toast.LENGTH_LONG).show(); //Output: "null"
return view;
}
//[......]
}
你应该使用 bundle 和 extras:
Fragment fragment = null;
fragment = new Menu1_1_Artikel();
if (fragment != null) {
Bundle args = new Bundle();
args.putString("link", link);
fragment.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
并收到 link ok Menu1_1_Artikel
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container, false);
Bundle bundle=getArguments();
link = bundle.getString("link");
Toast.makeText(getActivity(), "2.: "+link, Toast.LENGTH_LONG).show(); //Output: "null"
return view;
}
首先你必须让 "Menu1" class 实现 parcelable 或 serialisable,
然后,将参数设置为片段,
Bundle bundle = new Bundle();
bundle.putParcelable("obj", obj);
Fragment fragment = null;
fragment = new Menu1_1_Artikel();
fragment.setArguments(bundle);
然后,像下面这样在另一个片段中检索它,
Menu1 latitude = getArguments().getParcelable("obj")
(我想制作一个应用程序来显示新闻网站的 40 篇文章。)我在将字符串获取到另一个 class 时遇到问题。我的 "Menu1.java" 中有一个字符串 "link",我想在 "Menu1_1_Artikel.java" 中使用它。 Getter 方法有效。但我不知道如何设置 "link" 的值。每次我尝试它时,它只会得到 "null" 而不是在列表视图上单击的文章的 link 。 :/
public class Menu1 extends Fragment {
private ListView lv; //Listview which shows 40 articles from a news website
private ArrayAdapter adapter;
public ArrayList liste = new ArrayList();
private ProgressDialog progressDialog;
private String[] myStringArray = new String[40]; //Array with 40 URLs
public String link;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu_1, container, false);
lv = (ListView)view.findViewById(R.id.list);
adapter = new ArrayAdapter(getActivity(), R.layout.list_item, liste);
new datenAbrufen().execute(); //Method which loads the 40 articles
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
link = myStringArray[position]; //Save the URL to the article which is clicked on the listview to "link"
Menu1 obj = new Menu1();
obj.setLink(link); //I think here is the problem because the value is not in the Getter Method..
Toast.makeText(getActivity(), link, Toast.LENGTH_LONG).show(); //Output: Shows the URL from article -> "link" have a value
//Fragment
Fragment fragment = null;
fragment = new Menu1_1_Artikel();
if (fragment != null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
}
});
return view;
}
public String getLink(){
return this.link; //Problem: String "link" = null
}
public void setLink(String Str){
this.link= Str;
//Toast.makeText(getActivity(), link, Toast.LENGTH_LONG).show(); //Toast would show an Error: "Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference"
}
//[......]
}
public class Menu1_1_Artikel extends Fragment {
private String link;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container, false);
link= "Test";
Toast.makeText(getActivity(), "1.: "+link, Toast.LENGTH_LONG).show(); //Output: "Test"
Menu1 obj = new Menu1();
link= obj.getLink();
Toast.makeText(getActivity(), "2.: "+link, Toast.LENGTH_LONG).show(); //Output: "null"
return view;
}
//[......]
}
你应该使用 bundle 和 extras:
Fragment fragment = null;
fragment = new Menu1_1_Artikel();
if (fragment != null) {
Bundle args = new Bundle();
args.putString("link", link);
fragment.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
并收到 link ok Menu1_1_Artikel
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview, container, false);
Bundle bundle=getArguments();
link = bundle.getString("link");
Toast.makeText(getActivity(), "2.: "+link, Toast.LENGTH_LONG).show(); //Output: "null"
return view;
}
首先你必须让 "Menu1" class 实现 parcelable 或 serialisable,
然后,将参数设置为片段,
Bundle bundle = new Bundle();
bundle.putParcelable("obj", obj);
Fragment fragment = null;
fragment = new Menu1_1_Artikel();
fragment.setArguments(bundle);
然后,像下面这样在另一个片段中检索它,
Menu1 latitude = getArguments().getParcelable("obj")