android - 每次插入一个整数时,如何计算我在 EditText 中插入的整数的总和?

标签 android android-studio android-recyclerview integer

我有一个 RecyclerView,里面只有一个 TextView。我在它外面有一个添加按钮和一个 EditText,当我按下添加按钮时,它们只接受整数并将它们添加到 RecyclerView。我的问题是如何让它计算我插入到 EditText 中的所有整数的总和。

示例:假设我在第一行的 EditText 中输入 300 将显示 300,然后如果我在 EditText 中插入 200 我想在第二行中显示 500 (300 + 200) 等等。

这是我的代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.brecyclerview.MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edit_ten"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Score"
        android:inputType="numberSigned|number" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="btn_add"
            android:text="Add"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/btn_undo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="btn_undo"
            android:text="Undo"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/btn_new"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="btn_new"
            android:text="New Game" />

    </LinearLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleViewContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        tools:itemCount="8" />


</LinearLayout>

list_item.xml

  <RelativeLayout
      android:id="@+id/singleRow"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp">

      <ImageView
          android:id="@+id/userImg"
          android:src="@mipmap/ic_launcher"
          android:layout_width="60dp"
          android:layout_height="60dp" />

      <TextView
          android:id="@+id/pNametxt"
          android:text="User Name"
          android:textSize="20sp"
          android:layout_marginTop="6dp"
          android:maxLines="1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/userImg"
          android:layout_toEndOf="@+id/userImg"
          android:layout_marginLeft="8dp"
          android:layout_marginStart="8dp" />



  </RelativeLayout>

  <View
      android:layout_below="@+id/singleRow"
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="#f2f2f2" />

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button button;
    Button button1;
    Button button2;
    EditText editText;
    TextView personName;
    RecyclerView recyclerView;
    RecyclerView.Adapter mAdapter;
    RecyclerView.LayoutManager layoutManager;

    List<PersonUtils> personUtilsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.btn_add);
        button1 = (Button)findViewById(R.id.btn_undo);
        button2 = (Button)findViewById(R.id.btn_new);
        editText = (EditText)findViewById(R.id.edit_ten);
        personName = (TextView)findViewById(R.id.pNametxt);

        recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(layoutManager);

        personUtilsList = new ArrayList<>();



        //Adding Data into ArrayList


        mAdapter = new CustomRecyclerAdapter(this, personUtilsList);

        recyclerView.setAdapter(mAdapter);



        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == button2) {
                    MainActivity.this.finish();

                    startActivity(new Intent(MainActivity.this, MainActivity.class));
                }}});



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int pNametxt;
                int total = 0;
                for (int i = 0; i<personUtilsList.size(); i++)
                {
                    total += personUtilsList.get(i).getPersonName();
                }
                total = Integer.parseInt(editText.getText().toString());



                layoutManager = new LinearLayoutManager(getApplicationContext());
                recyclerView.setLayoutManager(layoutManager);
                personUtilsList.add(new PersonUtils(total));
                mAdapter = new CustomRecyclerAdapter(MainActivity.this, personUtilsList);
                recyclerView.setAdapter(mAdapter);
                editText.setText("");


            }});





    }}

CustomRecyclerAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;



public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<PersonUtils> personUtils;

    public CustomRecyclerAdapter(Context context, List personUtils) {
        this.context = context;
        this.personUtils = personUtils;
    }

    public static void remove(long i) {


    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemView.setTag(personUtils.get(position));

        PersonUtils pu = personUtils.get(position);


       holder.pName.setText(String.valueOf(pu.getPersonName()));



    }


    @Override
    public int getItemCount() {

        return personUtils.size();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder{

        public TextView pName;
        Button deleteButton;

        public ViewHolder(View itemView) {
            super(itemView);

            pName = (TextView) itemView.findViewById(R.id.pNametxt);
            deleteButton = (Button) itemView.findViewById(R.id.btn_undo);


                }



    }




}

PersonUtils.java

public class PersonUtils {

    private Integer personName;

    public Integer getPersonName() {
        return personName;
    }

    public void setPersonName(Integer personName) {
        this.personName = personName;
    }

    public PersonUtils(Integer personName) {
        this.personName = personName;
    }


}

提前谢谢你。

最佳答案

实际上,您是在代码中完成的,但有一个简单的错误。

按钮监听器中的 MainActivity.java

int total = 0;
for (int i = 0; i<personUtilsList.size(); i++)
{
    total += personUtilsList.get(i).getPersonName();
}
total = Integer.parseInt(editText.getText().toString());

它的问题是,您已经为 total 变量设置了一个新值,但它应该是 += 而不是 = 并且您的代码将按您希望的方式运行 InChaa'Allah。

但是我对你的编码方式有一些注意事项。

首先,这些行这样写是不好的。

layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
personUtilsList.add(new PersonUtils(total));
mAdapter = new CustomRecyclerAdapter(MainActivity.this, personUtilsList);
recyclerView.setAdapter(mAdapter);

会把所有的Activity生命周期都做一遍,会造成大量的CPU浪费。

所以你这样做。

((CustomRecyclerAdapter) mAdapter).addPersonUtils(new PersonUtils(total));
mAdapter.notifyDataSetChanged();//will make the RecyclerView adapter to reload its data.

在 CustomRecyclerAdapter.java 类中,我添加了一个新方法,用于向 personUtils ArrayList 添加一个新项目。

public void addPersonUtils(PersonUtils personUtil){
    personUtils.add(personUtil);
}

这样代码会更好。

关于android - 每次插入一个整数时,如何计算我在 EditText 中插入的整数的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469584/

相关文章:

java - 将变化的变量传递给 RecyclerView Adapter

android - 是否可以让 RecyclerView 在配置更改后恢复焦点项目?

Android 添加外部库到项目

java - 无法在 Android Studio 中打开布局

android - 如何在 Android studio 中更改新 Android 应用程序的图标?

java.lang.UnsatisfiedLinkError 和 ADB 中的未知错误

android - 尝试从配置 Activity 配置 widjget 时出现空指针异常

java - 我使用 shareDpreferences 将字符串保存在 textView 中,关闭应用程序并再次打开后,它不起作用

安卓 "skimlinks-unlinked"

android - 回收站 View 未填充宽度