0

I have the following scenario:

2 Fragments, one called: Add_DDC and the other called Add_Webview

In my Fragment Add_DDC, I have an EditText where I use:

final String new_ddc_name = ddc_name.getText().toString();

to get the text in the EditText.

With ddc_name_list.add(new_ddc_name); I want to add the text in from new_ddc_name to the array new_ddc_name(which I have added to my arrays.xml <string-array name="ddc_name_list" />.

Then in my Fragment Webview I've got a Spinner where I use the Adapter Function with my Array :

adapter = ArrayAdapter.createFromResource(getActivity(), R.array.ddc_name_list, android.R.layout.simple_spinner_item);

But the new entry in my Array ddc_name_listdoesn't apply to it.

I heard from adapter.notifyDataSetChanged(); but I don't where I have to add it.

Here is my code:

Add_DDC:

public class Add_DDC extends Fragment {

/* Anlegen der Variablen */
Button btn_add, btn_delete, btn_back;
EditText ddc_name, ddc_ip;
public static ArrayList<String> ddc_name_list = new ArrayList<>();
public static ArrayList<String> ddc_ip_list = new ArrayList<>();

public void onResume(){
    super.onResume();

    /* "ActionBar Title" festlegen */
    ((MainActivity) getActivity())
            .setActionBarTitle("DDC hinzufügen");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.add_ddc, container, false);

    ddc_name = (EditText) view.findViewById(R.id.edit_ddc_name);
    ddc_ip = (EditText) view.findViewById(R.id.edit_ddc_ip);

    final String new_ddc_name = ddc_name.getText().toString();
    final String new_ddc_ip = ddc_ip.getText().toString();

    /* Button "DDC HINZUFÜGEN" */
    btn_add = (Button) view.findViewById(R.id.btn_add);
    btn_add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            if(ddc_name.getText().length() != 0) {
                ddc_name_list.add(new_ddc_name);

            }

            if(ddc_ip.getText().length() != 0) {
                ddc_ip_list.add(new_ddc_ip);

            }

        }

    });

    /* Button "DDC LÖSCHEN" */
    btn_delete = (Button) view.findViewById(R.id.btn_delete);
    btn_delete.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {

            if(ddc_name.getText().length() != 0) {
                ddc_name_list.remove("new_ddc_name");
            }

        }

    });

    /* Button "Zurück" */
    btn_back = (Button) view.findViewById(R.id.btn_back);
    btn_back.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Fragment fr;

            fr = new Webview();

            FragmentManager fm = getFragmentManager();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_place, fr);
            fragmentTransaction.commit();
        }
    });

    return view;

}
}

Webview:

public class Webview extends Fragment {

WebView myWebView;
Spinner ddc_spinner;
public ArrayAdapter<CharSequence> adapter;

public void onResume(){
    super.onResume();

    /* "ActionBar Title" festlegen */
    ((MainActivity) getActivity())
            .setActionBarTitle("Start");

}

/* Neuladen der "WebView" Funktion verhindern
 * bei "Orientation change" (Portrait/Landscape) */
@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.webview, container, false);

    ddc_spinner = (Spinner) view.findViewById(R.id.ddc_spinner);

    adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.ddc_name_list, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ddc_spinner.setAdapter(adapter);

    ddc_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    /* WebView Einstellungen */
    myWebView = (WebView) view.findViewById(R.id.webView);
    myWebView.setVerticalScrollBarEnabled(true);
    myWebView.setHorizontalScrollBarEnabled(true);
    myWebView.setWebViewClient(new WebC());
    myWebView.getSettings().setDisplayZoomControls(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setSupportZoom(true);
    myWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.setInitialScale(0);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);

    /* Auswahl laden */
    myWebView.loadUrl("http://www.google.de");

    return view;
}


/* WebView Client implementieren */
public class WebC extends WebViewClient {

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        super.onReceivedError(view, errorCode, description, failingUrl);

    }
}

}

I hope you guys can help me to add my text to the spinner :)

EDIT:

I heard from the Callback interface, can I use this here?

Sorrow91
  • 69
  • 1
  • 1
  • 13
  • 1
    `adapter = ArrayAdapter.createFromResource(getActivity(), R.array.ddc_name_list, android.R.layout.simple_spinner_item);` will only get the strings that you have in the `string-array` from your XML. It will not know about the array you have in your other `Fragment`. Using `ddc_name_list.add(new_ddc_name);` will not add the new string to the `string-array`. You could save new strings to an `SQLiteDatabase` or `SharedPreferences`, then read that from the other `Fragment`. – Knossos Mar 31 '16 at 08:09
  • Can you give me an hint with the SharedPreferences with my code? :) – Sorrow91 Mar 31 '16 at 08:11
  • You could for example maintain a `JSONArray`. When you add a new item, you can open the `JSONArray` from `SharedPreferences`, add to the `JSONArray` then save it again to `SharedPreferences`. I won't go into that here, there are already [hundreds of examples of such things](http://stackoverflow.com/a/10241688/503508). – Knossos Mar 31 '16 at 08:17
  • @Knossos I read something about the `Callback` method but I didnt understand it right. Is that a possible solution too? Kannst es auch auf deutsch schreiben, sehe grade dass du aus Deutschland kommst :) – Sorrow91 Mar 31 '16 at 09:11

0 Answers0