java正在制作一个计算器应用程序,希望在第二个片段中更新历史,但无法完成
我是Android开发的初学者; 我正在制作一个计算器应用程序,希望在第二个片段中更新历史记录,但无法找到在第二个片段中更新ListView的完美解决方案。 我尝试了许多解决方案,但没有一个能奏效。 我正在使用ViewPager在计算器和历史片段之间滑动。 我厌倦了bundle类,但如果我在第一个片段(CalculatorFragment)的OnCreat方法中使用bundle和ViewPager,它会在启动应用程序后显示空白屏幕&;若我在Equal Button中使用Bundle类,它会使应用程序崩溃。 这里我将viewPager的id作为片段容器传递
我想要实现的强> 我想在点击“=”按钮时更新第二个片段(HistoryFragment)中的历史记录
以下是我迄今为止在代码中所做工作的示例代码强>
主要活动。java
package com.example.calculatorslide;
import 安卓x.annotation.RequiresApi;
import 安卓x.appcompat.app.ActionBar;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.viewpager.widget.ViewPager;
import 安卓.annotation.SuppressLint;
import 安卓.os.Build;
import 安卓.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
pageAdapter pageAdapter;
@RequiresApi(api = Build.VERSION_CODES.M)
@SuppressLint({"SetTextI18n", "UseCompatLoadingForDrawables"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.viewPager);
//Get rid of ActionBar
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.hide();
pageAdapter = new pageAdapter(getSupportFragmentManager(), 2);
viewPager.setAdapter(pageAdapter);
getSupportFragmentManager().beginTransaction().add(R.id.ViewPager,
new Calculatorfragment()).commit;
}
}
在CalculatorFragment中单击“相等”按钮时我正在执行的操作示例
public void Equals() {
String calc = etCalc.getText().toString();
if (calc.split("\\+").length == 2) {
String[] calculation = calc.split("\\+");
String Ans = String.valueOf(Double.parseDouble(calculation[0]) + Double.parseDouble(calculation[1]));
etCalc.setText(Ans);
tvCalc.setText(calc);
HistoryFragment fragment = new HistoryFragment();
Bundle bundle = new Bundle();
bundle.putString("key", calc+"="+Ans);
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.ViewPager, fragment).commit;
}
}
历史片段。java
package com.example.calculatorslide;
import 安卓.os.Bundle;
import 安卓x.fragment.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ListView;
import java.util.ArrayList;
public class HistoryFragment extends Fragment {
ListView history;
ArrayList<HistoryList> historyLists;
String History="";
View rootView;
public View onCreat(Bundle savedInstanceState){
Bundle bumdle = getArguments();
if (bundle != null)
String value = bundle.getString("key");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ArrayList<HistoryList> historyLists = new ArrayList<>();
if(History.split("=").length==2){
historyLists.add(new HistoryList(History.split("=")[0],History.split("=")[1]));
}
historyLists.add(new HistoryList("Example", "Example"));
historyLists.add(new HistoryList("23+5", "28"));
HistoryAdapter adapter = new HistoryAdapter(getActivity(), historyLists);
rootView = inflater.inflate(R.layout.fragment_history, container, false);
ListView listView = rootView.findViewById(R.id.history);
listView.setAdapter(adapter);
return rootView;
}
}
在HistoryList中,获取两个参数进行计算,并在ListView中显示答案
主要活动。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity">
<安卓x.viewpager.widget.ViewPager
安卓:id="@+id/viewPager"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
</LinearLayout>
共 (0) 个答案