簡體   English   中英

在父/其他活動和片段之間傳輸意圖/數據/包

[英]Transferring intent/ data/ bundle between parent/otherwise activity and fragment

我在這里有三個類:Ijob,Tabbed_Activity和PlaceholderFragment。 我想將在Ijob類中創建的字符串傳遞給我的PlaceholderFragment onCreateView,以將其顯示為自定義頁面。 我尚未創建第二個和第三個片段。 Ijob是一份工作清單。 Tabbed_Activity是片段的管理器類。 PlaceholderFragment是片段之一。 如您所見,我嘗試了bundle,intent和getActivity ..但它們似乎都不起作用。 我也嘗試過先將數據從Ijob移到Tabbed_Activity,然后移到PlaceholderFragment。 請幫忙。

Ijob.java

package com.example.administrator.signin;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
 import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

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

import java.util.ArrayList;
import java.util.HashMap;

public class Ijob extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON

//  private static String url = "http://0.0.00.00:xxxxx/Transporter/fetch_ffa_insp_emp.jsp?ecd=5d573aecfdc5e7a5";

// JSON Node names

private static final String TAG_CON_NO = "CONSUMER_NO";
private static final String TAG_MET_NO = "METER_NO";
private static final String TAG_REC_ON = "RECEIVED_ON";
private static final String TAG_CON_NAME = "CON_NAME";
//private static final String TAG_CON_CAT = "CON_CAT";
private static final String TAG_MET_RAT = "MET_RAT";
private static final String TAG_CON_ADD = "CON_ADD";
//private static final String TAG_CON_ACC_ADD = "CON_ACC_ADD";
private static final String TAG_COM_RSN = "COM_RSN";

public static final String TAG_M_REF = "M_REF";
private static final String TAG_M_DATE = "M_DATE";
//  //private static final String TAG_CON_CAT = " ";
private static final String TAG_PREV_DATA = "PREV_DATA";
private static final String TAG_TELE_NO = "TELE_NO";
private static final String TAG_IE_DT_N_PRSNT_PREV_RDG = "IE_DT_N_PRSNT_PREV_RDG";
private static final String TAG_LCC_LRO_ROM = "LCC_LRO_ROM";




// Hashmap for ListView
ArrayList<HashMap<String, String>> jobList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ijob);

    jobList = new ArrayList<HashMap<String, String>>();

    ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {



            // getting values from selected ListItem

            String CONSUMER_NO = ((TextView) view.findViewById(R.id.CON_NO))
                    .getText().toString();
            String METER_NO = ((TextView) view.findViewById(R.id.MET_NO))
                    .getText().toString();
            String MET_RAT = ((TextView) view.findViewById(R.id.MET_RAT))
                    .getText().toString();
            String REC_ON = ((TextView) view.findViewById(R.id.REC_ON))
                    .getText().toString();
            String CON_NAME = ((TextView) view.findViewById(R.id.CON_NAME))
                    .getText().toString();
            String CON_ADD = ((TextView) view.findViewById(R.id.CON_ADD))
                    .getText().toString();
            String COM_RSN = ((TextView) view.findViewById(R.id.COM_RSN))
                .getText().toString();
            String M_REF = ((TextView) view.findViewById(R.id.M_REF))
                    .getText().toString();
            String M_DATE = ((TextView) view.findViewById(R.id.M_DATE))
                    .getText().toString();
            String PREV_DATA = ((TextView) view.findViewById(R.id.PREV_DATA))
                    .getText().toString();
            String TELE_NO = ((TextView) view.findViewById(R.id.TELE_NO))
                    .getText().toString();
            String IE_DT_N_PRSNT_PREV_RDG = ((TextView) view.findViewById(R.id.IE_DT_N_PRSNT_PREV_RDG))
                    .getText().toString();
            String LCC_LRO_ROM = ((TextView) view.findViewById(R.id.LCC_LRO_ROM))
                    .getText().toString();


            // Starting single contact activity
                    Intent in = new Intent(getApplicationContext(), Tabbed_Activity.class);


        //  Bundle bundle= new Bundle();


        //  bundle.putString(TAG_CON_NO, CONSUMER_NO);
        //  bundle.putString(TAG_MET_NO, METER_NO);
        //  bundle.putString(TAG_MET_RAT, MET_RAT);
        //  bundle.putString(TAG_REC_ON, REC_ON);
        //  bundle.putString(TAG_CON_NAME, CON_NAME);
        //  bundle.putString(TAG_CON_ADD, CON_ADD);
        //  bundle.putString(TAG_COM_RSN, COM_RSN);
        //  bundle.putString(TAG_M_REF, M_REF);
        //  bundle.putString(TAG_M_DATE, M_DATE);
        //  bundle.putString(TAG_PREV_DATA, PREV_DATA);
        //  bundle.putString(TAG_TELE_NO, TELE_NO);
        //  bundle.putString(TAG_IE_DT_N_PRSNT_PREV_RDG, IE_DT_N_PRSNT_PREV_RDG);
        //  bundle.putString(TAG_LCC_LRO_ROM, LCC_LRO_ROM);

        //  PlaceholderFragment argumentFragment = new PlaceholderFragment();//Get Fragment Instance
         //   argumentFragment.setArguments(bundle);//Finally set argument bundle to fragment */



        //    in.putExtra(TAG_CON_NO, CONSUMER_NO);
        //  in.putExtra(TAG_MET_NO, METER_NO);
        //  in.putExtra(TAG_MET_RAT, MET_RAT);
        //  in.putExtra(TAG_REC_ON, REC_ON);
        //  in.putExtra(TAG_CON_NAME, CON_NAME);
        //  in.putExtra(TAG_CON_ADD, CON_ADD);
        //  in.putExtra(TAG_COM_RSN, COM_RSN);
            in.putExtra(TAG_M_REF, M_REF);
        //  in.putExtra(TAG_M_DATE, M_DATE);
        //  in.putExtra(TAG_PREV_DATA, PREV_DATA);
        //  in.putExtra(TAG_TELE_NO, TELE_NO);
        //  in.putExtra(TAG_IE_DT_N_PRSNT_PREV_RDG, IE_DT_N_PRSNT_PREV_RDG);
        //  in.putExtra(TAG_LCC_LRO_ROM, LCC_LRO_ROM); */



            startActivity(in);

        }
    });

    // Calling async task to get json
    new GetContacts().execute();
}


/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(Ijob.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String aid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        String url = "http://00.00.00.00:00000/Transporter/fetch_ffa_insp_emp_1.jsp?ecd=" + aid;
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
//              JSONObject jsonObj = new JSONObject(jsonStr);

                JSONArray jobs = new JSONArray(jsonStr);

                // Getting JSON Array node
//              jobs = jsonObj.getJSONArray("");

                // looping through All Contacts
                for (int i = 0; i < jobs.length(); i++) {
                    JSONObject c = jobs.getJSONObject(i);

                    String CON_NO = c.getString(TAG_CON_NO);//  +" - "+c.getString(TAG_CON_CAT);
                    String MET_NO = c.getString(TAG_MET_NO);
                    String MET_RAT = c.getString(TAG_MET_RAT);
                    String REC_ON = c.getString(TAG_REC_ON);
                    String CON_NAME = (i + 1) + " . " + c.getString(TAG_CON_NAME);
                    String CON_ADD = c.getString(TAG_CON_ADD); //+" "+c.getString(TAG_CON_ACC_ADD);
                    String COM_RSN = c.getString(TAG_COM_RSN);
                    String M_REF = c.getString(TAG_M_REF);
                    String M_DATE = c.getString(TAG_M_DATE);
                    String PREV_DATA = c.getString(TAG_PREV_DATA);
                    String TELE_NO = c.getString(TAG_TELE_NO);
                    String IE_DT_N_PRSNT_PREV_RDG = c.getString(TAG_IE_DT_N_PRSNT_PREV_RDG);
                    String LCC_LRO_ROM = c.getString(TAG_LCC_LRO_ROM);

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_CON_NO, CON_NO);
                    contact.put(TAG_MET_NO, MET_NO);
                    contact.put(TAG_MET_RAT, MET_RAT);
                    contact.put(TAG_REC_ON, REC_ON);
                    contact.put(TAG_CON_NAME, CON_NAME);
                    contact.put(TAG_CON_ADD, CON_ADD);
                    contact.put(TAG_COM_RSN, COM_RSN);

                    contact.put(TAG_M_REF, M_REF);
                    contact.put(TAG_M_DATE, M_DATE);
                    contact.put(TAG_PREV_DATA, PREV_DATA);
                    contact.put(TAG_TELE_NO, TELE_NO);
                    contact.put(TAG_IE_DT_N_PRSNT_PREV_RDG, IE_DT_N_PRSNT_PREV_RDG);
                    contact.put(TAG_LCC_LRO_ROM, LCC_LRO_ROM);

                    // adding contact to contact list
                    jobList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                Ijob.this, jobList,
                R.layout.list_item, new String[]{TAG_CON_NO, TAG_MET_NO, TAG_MET_RAT,
                TAG_REC_ON, TAG_CON_NAME, TAG_CON_ADD, TAG_COM_RSN, TAG_M_REF, TAG_M_DATE, TAG_PREV_DATA, TAG_TELE_NO, TAG_IE_DT_N_PRSNT_PREV_RDG, TAG_LCC_LRO_ROM}, new int[]{R.id.CON_NO,
                R.id.MET_NO, R.id.MET_RAT, R.id.REC_ON, R.id.CON_NAME, R.id.CON_ADD, R.id.COM_RSN, R.id.M_REF, R.id.M_DATE, R.id.PREV_DATA, R.id.TELE_NO, R.id.IE_DT_N_PRSNT_PREV_RDG, R.id.LCC_LRO_ROM});

        setListAdapter(adapter);
    }

}



}

Tabbed_Activity.java

package com.example.administrator.signin;


import android.content.Intent;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

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.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

public class Tabbed_Activity extends AppCompatActivity {


    public static final String TAG_M_REF = "M_REF";


    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

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


    Intent in = getIntent();
    String MET_REF = in.getStringExtra(TAG_M_REF);
    Bundle bundle = new Bundle();
    bundle.putString("edttext", MET_REF);
    PlaceholderFragment argumentFragment = new PlaceholderFragment();//Get Fragment Instance
    argumentFragment.setArguments(bundle);//Finally set argument bundle to fragment */


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}


@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_tabbed_, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).


        return PlaceholderFragment.newInstance(position);


    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;
    }
}
}

PlaceholderFragment.java

package com.example.administrator.signin;

/**
 * Created by Administrator on 8/26/2016.
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
 * Returns a new instance of this fragment for the given section
 * number.
 */
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.placeholder_1, container, false);




   String strtext = getArguments().getString("edttext");


    TextView textView = (TextView) rootView.findViewById(R.id.m_ref);


    textView.setText(strtext);
    return rootView;
}
}

似乎您沒有使用在活動oncreate方法中創建的占位符片段。 您僅在此處設置包。 在viewpager中創建的PlaceHolderFragments沒有捆綁。 您必須在viewpager中設置包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM