【问题标题】:How to calculate the sum of the Integers I insert in EditText everytime I insert an Integer?每次插入整数时如何计算我在 EditText 中插入的整数的总和?
【发布时间】:2020-02-16 13:21:26
【问题描述】:

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

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

这是我的代码

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;
    }


}

提前谢谢你。

【问题讨论】:

    标签: android android-studio android-recyclerview integer


    【解决方案1】:

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

    按钮监听器中的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);
    }
    

    这样代码会更好。

    【讨论】:

    • 非常感谢你,这很有帮助,但我想要的是当我向 edittext 插入一个整数时,我希望它像这样添加到 recyclerciew 中的最后一个值:首先我插入 200 它将是在第一行然后我插入 300 所以第二行将是 500 然后假设我插入了 200 它将是 700,但是你帮助它总结了 recyclerview 中的所有值我希望你能帮助我,谢谢你。
    • 不客气,艾哈迈德,祝你好运。
    【解决方案2】:

    当用户单击添加按钮时,您可以从数组列表中获取最后一个值,并在编辑文本处与值相加。将总和添加到您的列表中。

    您不必再这样做了。

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

    只需将您的项目添加到 personUtilsList 并调用 recyclerView.notifyDataSetChanged() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多