获取另一个活动的字符串

时间:2017-11-23 16:26:41

标签: java android setter getter

(我想制作一个应用程序来显示一篇新闻网站的40篇文章。)我有一个问题,即将String转换为另一个类。我有一个String"链接"在我的" Menu1.java"并希望在" Menu1_1_Artikel.java"中使用它。 Getter方法正在运行。但我不知道如何设置"链接"的价值。每次我尝试它只会得到" null"而不是在列表视图上单击的文章的链接。 :/

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;
}
//[......]

}

1 个答案:

答案 0 :(得分:1)

首先你必须制作&#34; Menu1&#34;用于实现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")