忍受我,这是我的第一个Android应用程序 我需要帮助将arraylist(n)从片段转移到另一个(frag1到frag2)。 我读了关于捆绑和接口但我无法实现这些解决方案。 任何详细的解决方案都会有所帮助。
这是我的主要活动
@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) {
}
这是我的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;
}
这是我的frag2
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View frag2v = inflater.inflate(R.layout.activity_frag2, container, false);
return frag2v;
}
答案 0 :(得分:-1)
由于你的问题是你想知道如何使用意图发送它,这篇文章解决了这个问题:Pass ArrayList<Double> from one activity to another activity on android
代码示例:
FirstActivity:
Intent intent = new Intent(this, OtherActivity.class);
ArrayList<Double> listDouble = new ArrayList<Double>();
listDouble.add(1111.00000);
listDouble.add(13331.00000);
intent.putExtra("arraylist", listDouble);
startActivity(intent);
OtherActivity :(检索Double ArrayList)
ArrayList<Double> listDouble = (ArrayList<Double>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.." + listDouble);
对于片段通讯,请查看以下帖子:Basic Communication between two fragments或官方文档:https://developer.android.com/training/basics/fragments/communicating.html
public class YourActivity implements FragmentA.TextClicked{ @Override public void sendText(String text){ // Get Fragment B FraB frag = (FragB) getSupportFragmentManager().findFragmentById(R.id.fragment_b); frag.updateText(text); }
}
// Fragment A定义一个接口,并在需要时调用该方法 公共类FragA扩展了Fragment {
TextClicked mCallback;
public interface TextClicked{
public void sendText(String text);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (TextClicked) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement TextClicked");
}
}
public void someMethod(){
mCallback.sendText("YOUR TEXT");
}
@Override
public void onDetach() {
mCallback = null; // => avoid leaking, thanks @Deepscorn
super.onDetach();
}
}
//片段B有一个公共方法来处理文本 公共类FragB扩展了Fragment {
public void updateText(String text){
// Here you have it
}
}