java—制作计算器应用程序,希望在第二个片段中更新历史,但无法完成

6kkfgxo0  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(232)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

5天前关门了。
改进这个问题
我是android开发的初学者&我正在制作一个计算器应用程序,希望在第二个片段中更新历史,但是没有找到在第二个片段中更新listview的完美解决方案。我试过许多解决办法,但都不管用。我用viewpager在计算器和历史片段之间滑动。我厌倦了bundle类,但如果我在第一个片段(calculatorfragment)的oncreat方法中使用bundle和viewpager,它会在启动应用程序后显示一个空白屏幕&如果我在equal button中使用bundle类,它会使应用程序崩溃。这里我传递viewpager的id作为片段的容器。
我想要得到的。当点击“=”按钮时,我想在第二个片段(historyfragment)中更新历史。
下面是我在代码中所做工作的示例代码。
主活动.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中单击相等按钮时我正在执行的操作的示例

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 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中显示答案。
活动\u 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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题