java - 制作计算器应用程序并希望在第二个片段中更新历史记录但无法做到
问题描述
我是 Android 开发的初学者,我正在制作一个计算器应用程序,想在第二个片段中更新历史记录,但无法找到在第二个片段中更新 ListView 的完美解决方案。我尝试了许多解决方案,但没有一个能正常工作。我正在使用 ViewPager 在计算器和历史片段之间滑动。我厌倦了 bundle 类,但是如果我在第一个 Fragment(CalculatorFragment) 的 OnCreat 方法中使用带有 ViewPager 的 bundle,它会在启动应用程序后显示空白屏幕,如果我在 Equal Button 中使用 Bundle 类,它会使应用程序崩溃。在这里,我将 viewPager 的 id 作为片段的容器传递。
我想要达到的目标。 我想在单击“=”按钮时更新第二个片段(HistoryFragment)中的历史记录。
这是到目前为止我在代码中所做的示例代码。
MainActivity.java
package com.example.calculatorslide;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.annotation.SuppressLint;
import android.os.Build;
import android.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 中单击 Equal 按钮时我在做什么的示例
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;
}
}
HistoryFragment.java
package com.example.calculatorslide;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.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;
}
}
在 historyLists 中,这里采用两个参数进行计算,并在 ListView 中显示答案。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
解决方案
推荐阅读
- java - 如何在 Android 11 中列出特定目录中的媒体文件?
- react-native - 如何呈现 SectionList Header?
- python - 程序应该产生一个输出值
- python - ox.stats.basic_stats 在 ox.graph.graph_from_place 更改 clean_periphery 时出错
- orocrm - Oro Commerce 页面组件
- python - 如何解决输出错误(最大子数组问题)?
- ios - 从 UINavigationController 中的第一个控制器返回
- html - 使用带有水平溢出的 KaTeX 时避免垂直溢出
- python - 我想在图形中制作闪烁的点作为程序
- javascript - 有没有办法可以将计算机中的音频文件与使用网络音频的服务器中的音频文件混合?