在Android应用中将数据从Activity发送到片段

时间:2016-05-01 18:39:43

标签: android android-activity fragment

在我的应用程序中,我有一个Activity(MainScreen)和一个Fragment(Orders)。我在Activity中有一个字符串(名称),我想将它发送到Fragment。 (在注释行下,您可以看到我尝试将字符串从活动发送到片段)。我不知道为什么,但应用程序崩溃了。

这是MainScreen.java:

public class MainScreen  extends AppCompatActivity {

    Switch list_toggle;
    String user_id,username,status;
    boolean ischecked;


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

        final TextView textView4 = (TextView) findViewById(R.id.textView4);
        final TextView textView3 = (TextView) findViewById(R.id.textView3);

        list_toggle = (Switch) findViewById(R.id.mySwitch);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {

            username = extras.getString("USER_NAME");
            user_id = extras.getString("USER_ID");
            status = extras.getString("STATUS");
        }

        if (status.contentEquals("Available")){

            ischecked = true;
        }
        else{
            ischecked = false;
        }

        textView4.setText(Html.fromHtml(status));
        list_toggle.setChecked(ischecked);


        String[] parts = username.split(" ");
        String name = parts[0]; // 004
        String surname = parts[1];

        textView3.setText(Html.fromHtml("Hi, " + name + " "));


        list_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {

                    textView4.setText(Html.fromHtml("Available"));

                } else {

                    textView4.setText(Html.fromHtml("Unavailable"));

                }
            }
        });

        //I tried to do this to send "name" to the fragment
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        Orders fragobj = new Orders();
        fragobj.setArguments(bundle);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Orders"));
        tabLayout.addTab(tabLayout.newTab().setText("Past Orders"));
        tabLayout.addTab(tabLayout.newTab().setText("More"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {


            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

这是Orders.java:

public class Orders extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.orders, container, false);
        ListView listView1 = (ListView) v.findViewById(R.id.listView1);
        TextView textView3 = (TextView) v.findViewById(R.id.textView16);


        String currentDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
        textView3.setText(Html.fromHtml(currentDate));

        //I tried to do this to get the String name from the Activity
        String data = getArguments().getString("name");
        System.out.println(data);

        Order[] items = {
                new Order("#403", "07-04-2016", "5:29 PM"),
                new Order("#404", "07-04-2016", "5:35 PM"),
                new Order("#405", "07-04-2016", "5:40 PM"),
                new Order("#406", "07-04-2016", "5:54 PM"),
        };

        ArrayAdapter<Order> adapter = new ArrayAdapter<Order>(getActivity(),
                android.R.layout.simple_list_item_1, items);

        listView1.setAdapter(adapter);

        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                String item = ((TextView) view).getText().toString();


                Intent intent = new Intent(getActivity(), OrderDetails.class);
                intent.putExtra("data",item);
                startActivity(intent);

            }
        });

        return v;
    }
}

1 个答案:

答案 0 :(得分:0)

一种最简单的方法是将'name'定义为public static变量,并在fragment类中使用String data=MainScreen.name;