如何在活动和其他活动的片段之间导航数据?

时间:2016-01-08 09:07:58

标签: android listview android-fragments tabs

这可能是一个愚蠢的问题,但它仍然混淆如何将导航数据从活动传递到其他活动的片段我怎么能实现这一点我已经搜索和累了仍然我找不到任何可以有人帮助我。现在让我告诉我的要求我在listview中有tabactivity我有listview当我点击listivew项目时它必须转到其他活动,数据将去,并填写listview中的列表视图我怎么能实现这个到目前为止我尝试过是 这是我的标签活动: package servicefiirst.precision.activitiestabs;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    /**
     * 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_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // 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_main, 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 placeholder fragment containing a simple view.
     */


    /**
     * 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).
                switch (position){
                    case 0:
                            Task task=new Task();
                            return task;

                    case 1:
                            Calls calls=new Calls();
                            return calls;

                }
        return null;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
           switch (position){
               case 0:
                   return "Task";
               case 1:
                   return "Call";

            }
        return null;
            }

        }
    }

这是listview getspopulated的片段:

package servicefiirst.precision.activitiestabs;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by 4264 on 08-01-2016.
 */
public class Task extends Fragment {

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

        View rootview= inflater.inflate(R.layout.yog,container,false);

         Listview lv=(Listview)rootview.findviewbyId(R.id.lv);
        lv.setOnItemClickListener(new OnItemClickListener(
           @Override
        public void onClick(View v) {
          Intent intent=new Intent(getActivity,DetailActivity.class)
          startactivity(intent);
        }
    });



        return rootview;
    }




}

2 个答案:

答案 0 :(得分:2)

使用捆绑包将数据从一个活动转移到另一个活动

Bundle bundle = new Bundle();
bundle.putString("KEY_NAME", "Abrakadabra");
Intent i = new Intent(this, MyActivityName.class);
i.putExtras(bundle);
startActivity(i)  <-- new activity started

然后在接收活动中:将此代码放入onCreate方法

Bundle bundle = getIntent().getExtras();
String stringdata = bundle.getString("KEY_NAME"); 

将数据从活动传递到片段:将此代码放在任何地方

Bundle bundle = new Bundle();
bundle.putString("KEY_NAME", "Abrakadabra");
MyFragment myfragment = new MyFragment();
myfragment.setArguments(bundle);

然后在片段的onCreateView方法中添加此代码

Bundle args = getArguments();
String stringdata = args.getString("KEY_NAME"); 

答案 1 :(得分:0)

尝试将此数据发送到其他活动

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView c = (TextView) view.findViewById(R.id.label);
                String source = c.getText().toString();
                Intent intent = new Intent(MetroFrom.this, Metro.class);
                intent.putExtra("From", source);
                startActivity(intent);


            }
        });

然后将此代码放在您想要接收数据的活动

Intent intent = getIntent();
        etsource.setText(intent.getStringExtra("from"));