【问题标题】:How to navigate data between activity and fragment of other activity?如何在活动和其他活动的片段之间导航数据?
【发布时间】:2016-01-08 09:07:58
【问题描述】:

这可能是一个愚蠢的问题,但它仍然令人困惑如何将导航数据从活动传递到其他活动的片段我怎样才能实现这一点我已经搜索过并且很累我仍然找不到任何可以帮助我的东西。现在让我告诉我我的要求是在我有列表视图的选项卡之一中具有选项卡活动,当我单击 listivew 项目时,它必须转到数据将进入的其他活动并在选项卡中填充列表视图到目前为止我如何实现这一点我已经尝试过是 这是我的标签活动: 包 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;
            }

        }
    }

这是填充列表视图的片段:

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;
    }




}

【问题讨论】:

  • 只需在您的意图中添加一个指示符intent.putExtra("flag", positionOfYourClickedItem),在下一个Activity 中使用BundlegetIntent().getExtras() 检索它,然后在其中加载您的数据。
  • 我的疑问是需要从一个活动传递对象并需要在另一个活动的片段中填充列表视图

标签: android listview android-fragments tabs


【解决方案1】:

使用捆绑包将数据从一个活动传输到另一个活动

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"); 

【讨论】:

  • 这对于字符串来说没问题,但我需要传递对象来填充列表我怎么会这样做
  • 使用 putStringArrayList 和 getStringArrayList 分别作为列表检查此链接,如果您使用循环传递图像、音频作为捆绑包,然后使用循环将它们提取回列表stackoverflow.com/questions/10975239/…
  • 如果解决了查询中提到的问题,请将其标记为已回答。将来会帮助其他人
【解决方案2】:

尝试将数据发送到另一个活动

 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);


            }
        });

然后把这段代码放在你想接收数据的activity上

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

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多