Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

send Arraylist between two fragments

alimanasraalimanasra JordanPosts: 1
edited June 2016 in Assembler Developer

bear with me, this is my first android app and i need help to transfer arraylist (n) from fragment to another (frag1 to frag2). i read about bundle and interface but i could not implement those solutions. any detailed solution would be helpful.

here is my main acitvity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Intent intent = getIntent();
String message = intent.getStringExtra("array_list");

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setTabTextColors(R.color.colorPrimaryDark, R.color.colorAccent);
tabLayout.addTab(tabLayout.newTab().setText("Split").setIcon(R.drawable.money));
tabLayout.addTab(tabLayout.newTab().setText("Item List").setIcon(R.drawable.list));
tabLayout.addTab(tabLayout.newTab().setText("Rates").setIcon(R.drawable.settings));
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) {

    }

here is my frag1

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View frag1v = inflater.inflate(R.layout.activity_frag1, container, false);

//Add Button//
Button b = (Button) frag1v.findViewById(R.id.add_btn);
final ArrayList<Double> n = new ArrayList<Double>();
final EditText e = (EditText) frag1v.findViewById(R.id.sum);
final EditText d = (EditText) frag1v.findViewById(R.id.editText3);
final TextView tx = (TextView) frag1v.findViewById(R.id.textView4);
final Double div = Double.parseDouble(d.getText().toString());

// Shared Setting //
SharedPreferences Setting_pre = getActivity().getSharedPreferences("Setting", Context.MODE_PRIVATE);
final String Discount_shared = Setting_pre.getString("Discount", "0");
final String Service_shared = Setting_pre.getString("Service", "1.1");
final String Tax_shared = Setting_pre.getString("Tax", "1.16");


b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (e.getText().length() > 0) {
            Double e_double = Double.parseDouble(e.getText().toString());
            Double div = Double.parseDouble(d.getText().toString());
            Double sum_d = e_double / div;
            n.add(sum_d);
        }
        Double sum = 0.0;
        for (int i = 0; i < n.size(); i++) {
            sum += n.get(i);
        }
        e.setText("");

        if (Discount_shared.equals("0")) {
            Double Discount_M = 1.0;
            Double sum_d = sum / div;
            Double Discount_Sum = sum_d * Discount_M;
            Double Service_M = Double.parseDouble(Service_shared.toString());
            Double Service_Sum = Discount_Sum * Service_M;
            Double Tax_M = Double.parseDouble(Tax_shared.toString());
            Double Tax_Sum = Service_Sum * Tax_M;
            String Total = Tax_Sum.toString().format("%.2f", Tax_Sum);
            tx.setText(Total);
        } else {
            Double Discount_M = Double.parseDouble(Discount_shared.toString());
            Double sum_d = sum / div;
            Double Discount_Sum = sum_d * Discount_M;
            Double Service_M = Double.parseDouble(Service_shared.toString());
            Double Service_Sum = Discount_Sum * Service_M;
            Double Tax_M = Double.parseDouble(Tax_shared.toString());
            Double Tax_Sum = Service_Sum * Tax_M;
            String Total = Tax_Sum.toString().format("%.2f", Tax_Sum);
            tx.setText(Total);
        }


    }
});

//clear//
Button c = (Button) frag1v.findViewById(R.id.clear_btn);
c.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        n.clear();
        tx.setText("0");
    }
});


return frag1v;

}
here is my frag2

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 View frag2v = inflater.inflate(R.layout.activity_frag2, container, false);



return frag2v;

}

Tagged:
Sign In or Register to comment.