通过2个片段

时间:2015-11-15 21:36:17

标签: java android android-fragments arraylist interface

嘿我尝试在一个活动中使用ArrayList方法在两个Interface之间传递Fragments。 logcat表示没有错误,但我在ArrayList中添加了Fragment的项目未显示在Spinner的另一个Fragment中。 我会发布我的整个代码,所以如果你看到我的问题,如果你解释我的解决方案会很好。提前谢谢。

MainActivity.java

 package de.kwietzorek.fulcrumwebview;


import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements Callback {

    static WebView myWebView;
    Fragment AddServerFragment, WebViewFragment;
    ArrayList<String> server_name_list;

    @Override
    public ArrayList<String> getArrayListFromActivity() {

        WebViewFragment frag = (WebViewFragment) getSupportFragmentManager().findFragmentByTag("AddServerFragment");
        if (frag != null) {
            server_name_list = frag.getArrayListFromActivity();
        }
        return server_name_list;
    }

    /* Menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.webview:
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, new WebViewFragment() {

                        })
                        .commit();
                return true;
            case R.id.add_server:
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, new AddServerFragment() {

                        })
                        .commit();
                return true;
            case R.id.menu_refresh:
                myWebView.reload();
                return true;
            default:
                return true;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (findViewById(R.id.container) != null) {
            if (savedInstanceState != null) {
                return;
            }
            AddServerFragment = new Fragment();
            WebViewFragment = new Fragment();
        }
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new WebViewFragment() {

                })
                .commit();
    }

    /*WebView Fragment*/
    public class WebViewFragment extends Fragment {

        String selected;
        Spinner spinner;

        public WebViewFragment(){
        }

        private Callback callback;


        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            callback = (Callback) activity;}

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

            /* WebView */
            myWebView = (WebView) view.findViewById(R.id.webView);
            myWebView.setWebViewClient(new WebC());
            WebSettings webSettings = myWebView.getSettings();

            /* JavaScript Enable */
            webSettings.setJavaScriptEnabled(true);

            /* Server Name Spinner */

            ArrayList<String> server_name_list = callback.getArrayListFromActivity();

            if (server_name_list != null) {

                spinner = (Spinner) view.findViewById(R.id.server_spinner);
                ;
                ArrayAdapter<String> server_adapter = new ArrayAdapter<String>(this.getActivity(),
                        android.R.layout.simple_spinner_item, server_name_list);

                server_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner.setAdapter(server_adapter);

                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                        selected = parent.getItemAtPosition(pos).toString();

                        myWebView.loadUrl(selected);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
            }
            return view;
        }

        public ArrayList<String> getArrayListFromActivity() {
            return server_name_list;
        }
    }

    /*Add Server Fragment*/
    public class AddServerFragment extends Fragment
            implements View.OnClickListener {

        @Override
        public void onClick(View view) {
        }

        public AddServerFragment(){
        }

        Button btn_back, btn_add;
        EditText server_ip, server_name;

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

            server_ip = (EditText) view.findViewById(R.id.edit_server_address);
            server_name = (EditText) view.findViewById(R.id.edit_server_name);

            btn_back = (Button) view.findViewById(R.id.btn_back);
            btn_back.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.container, new WebViewFragment() {

                            })
                            .commit();
                }
            });

            btn_add = (Button) view.findViewById(R.id.btn_add);
            btn_add.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    String new_server_ip = null, new_server_name = null;

                    ArrayList<String> server_name_list = new ArrayList<>();
                    ArrayList<String> server_ip_list = new ArrayList<>();

                    new_server_ip = server_ip.getText().toString();
                    server_ip_list.add(new_server_ip);

                    new_server_name = server_name.getText().toString();
                    server_name_list.add(new_server_name);
                }
            });

            return view;
        }
    }

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

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

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

        }
    }
}

Callback.java

 package de.kwietzorek.fulcrumwebview;

import java.util.ArrayList;

public interface Callback {
        ArrayList<String> getArrayListFromActivity();
    }

1 个答案:

答案 0 :(得分:0)

每次你将某些东西添加到某个Adapter使用的ArrayList中(在你的情况下是由微调器的适配器),你必须在它上面调用adapter.notifyDataSetChanged()。所以你必须告诉&#34;它需要刷新的WebViewFragment(在适配器上调用notifyDataSetChanged())每次你在第二个片段中向ArrayList添加内容时它的内容