Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将数据从活动传递到第一次未加载的片段_Android_Fragment - Fatal编程技术网

Android 将数据从活动传递到第一次未加载的片段

Android 将数据从活动传递到第一次未加载的片段,android,fragment,Android,Fragment,我试图将数据从一个活动传递到一个片段。我有四张账单。数据已传递,但第一次未显示,当我滑动其他三个选项卡并返回到第一个选项卡时,数据将显示。我在活动的onCreate方法中调用JSON数据解析方法。这里我提供我的代码。 这是Main@Activity.java package com.example.iqbal.privateuniversityinfo; import android.os.Bundle; import android.support.design.widget.Tab

我试图将数据从一个活动传递到一个片段。我有四张账单。数据已传递,但第一次未显示,当我滑动其他三个选项卡并返回到第一个选项卡时,数据将显示。我在活动的onCreate方法中调用JSON数据解析方法。这里我提供我的代码。 这是Main@Activity.java

    package com.example.iqbal.privateuniversityinfo;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

// https://api.myjson.com/bins/lb6b8

// new https://api.myjson.com/bins/1gzo6k
public class Main2Activity extends AppCompatActivity {
    String name, fac_and_sub, tution_fees, admission_info, contact;
    private String div;
    private int pos;

    private RequestQueue mqueue;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private Bundle bundle;

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

        div = getIntent().getStringExtra("div");
        pos = Integer.parseInt(getIntent().getStringExtra("pos"));

        bundle = new Bundle();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        jsonParse();
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.university_details, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {


        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {

                case 0:
                    FacultyAndSubject facultyAndSubject = new FacultyAndSubject();
                    facultyAndSubject.setArguments(bundle);
                    return facultyAndSubject;
                case 1:
                    TutionFees tutionFees = new TutionFees();
                    return tutionFees;
                case 2:
                    Admission_info admission_info = new Admission_info();
                    return admission_info;
                case 3:
                    Contact contact = new Contact();
                    return contact;

                default:
                    return null;
            }


        }

        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }
    }

    private void jsonParse() {
        String url = "https://api.myjson.com/bins/1gzo6k";
        mqueue = Volley.newRequestQueue(this);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject jsonObject = response.getJSONObject(i);

                        switch (div) {
                            case "ctg":
                                if (i == pos) {
                                    name = jsonObject.getString("name");
                                    fac_and_sub = jsonObject.getString("faculty_and_sub");
                                    tution_fees = jsonObject.getString("tution_fees");
                                    admission_info = jsonObject.getString("admission_info");
                                    contact = jsonObject.getString("contact");

                                    bundle.putString("name", name);
                                    bundle.putString("fac", fac_and_sub);
                                    bundle.putString("fees", tution_fees);
                                    bundle.putString("info", admission_info);
                                    bundle.putString("contact", contact);
                                }
                                break;


                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        mqueue.add(jsonArrayRequest);
    }
}

尝试将代码添加到
开关的
案例0
中,与默认案例相同


同样按照@babich的建议,请在收到响应后尝试设置适配器。

是否需要在收到数据后设置viewPager? 将完成日志消息添加到您的
onResponse
end,并将其添加到
FacultyAndSubject的
onCreateView

而不是提前检查电话。 您知道如何添加日志消息以及如何使用
LogCat

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    FacultyAndSubject facultyAndSubject;
    TutionFees tutionFees;
    Admission_info admission_info;
    Contact contact;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {

            case 0:
                if(facultyAndSubject == null) {
                facultyAndSubject = new FacultyAndSubject();
                facultyAndSubject.setArguments(bundle);
                }
                return facultyAndSubject;
            case 1:
                if(tutionFees == null) tutionFees = new TutionFees();
                return tutionFees;
            case 2:
                if(admission_info == null) admission_info = new Admission_info();
                return admission_info;
            case 3:
                if(contact == null) contact = new Contact();
                return contact;

            default:
                return null;
        }


    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }
}
第一:

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.faculty_and_subject, container, false);
        tv = v.findViewById(R.id.tv);

        if (getArguments() != null) {
            this.name = getArguments().getString("name");
            this.fac = getArguments().getString("fac");
            this.fees = getArguments().getString("fees");
            this.info = getArguments().getString("info");
            this.contact = getArguments().getString("contact");
            updateData();
        } else showLoading();

        tv.setText(name + "\n" + " " + fac + "\n" + fees + "\n" + info + "\n" + contact);

        return v;
    }

    public void updateData(String name, String fac, String fees, String info, String contact) {
    stopLoading();
    this.name = name;
    this.fac = fac;
    this.fees = fees;
    this.info = info;
    this.contact = contact;
    } 
第二点:

        @Override
        public void onResponse(JSONArray response) {

            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);

                    switch (div) {
                        case "ctg":
                            if (i == pos) {
                                name = jsonObject.getString("name");
                                fac_and_sub = jsonObject.getString("faculty_and_sub");
                                tution_fees = jsonObject.getString("tution_fees");
                                admission_info = jsonObject.getString("admission_info");
                                contact = jsonObject.getString("contact");
                                if(mSectionsPagerAdapter.facultyAndSubject != null) {


                                mSectionsPagerAdapter.facultyAndSubject.
                                updateDate(name, fac, fees, info, contact) 
                                } else  {
                                mSectionsPagerAdapter.facultyAndSubject = new FacultyAndSubject();
            Bundle bundle = new Bundle();
            bundle.putString("name", name);
                                bundle.putString("fac", fac_and_sub);
                                bundle.putString("fees", tution_fees);
                                bundle.putString("info", admission_info);
                                bundle.putString("contact", contact);
            facultyAndSubject.setArguments(bundle);
                                }
                            }
                            break;


                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }
@覆盖
公共void onResponse(JSONArray响应){
对于(int i=0;i
09-04 00:27:51.175 6747-6747/com.example.iqbal.private university信息E/AAA:FacultyAndSubject call 09-04 00:28:02.386 6747-6747/com.example.iqbal.private university信息E/AAA:onResponse finishedFirst调用FacultyAndSubject这就是它第一次变为空的原因你能告诉我如何设置viewPager吗?@zhiki认为你必须检查一下吗在FacultyAndSubject中,没有数据,如果仍然没有数据,则显示加载微调器。在这种情况下,这是最好的解决方案。还有一件事。如果全局声明片段,并在
fragment==null
时创建它们,效果会更好。在
jsonParse
中再次检查
FacultyAndSubject
是否为空。如果为空,则使用数据创建它;如果不为空,则更新数据。
        @Override
        public void onResponse(JSONArray response) {

            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);

                    switch (div) {
                        case "ctg":
                            if (i == pos) {
                                name = jsonObject.getString("name");
                                fac_and_sub = jsonObject.getString("faculty_and_sub");
                                tution_fees = jsonObject.getString("tution_fees");
                                admission_info = jsonObject.getString("admission_info");
                                contact = jsonObject.getString("contact");
                                if(mSectionsPagerAdapter.facultyAndSubject != null) {


                                mSectionsPagerAdapter.facultyAndSubject.
                                updateDate(name, fac, fees, info, contact) 
                                } else  {
                                mSectionsPagerAdapter.facultyAndSubject = new FacultyAndSubject();
            Bundle bundle = new Bundle();
            bundle.putString("name", name);
                                bundle.putString("fac", fac_and_sub);
                                bundle.putString("fees", tution_fees);
                                bundle.putString("info", admission_info);
                                bundle.putString("contact", contact);
            facultyAndSubject.setArguments(bundle);
                                }
                            }
                            break;


                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }