Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have create 2 listviews which shows budget income and expenses.
I manage to get its total amount individually in each listview and shown it at their textview.

Now, i have difficulty to get the remaining cash which is the total amount from listview1 minus total amount from listview2 and to show it as textview tvtotal3.



MainActivity.java
Java
<pre>

public class MainActivity extends AppCompatActivity {

  String totalPricefinal = "" ;
  int value1,value2,value3;


   TextView tvtotal,tvtotal2,tvtotal3;

    EditText etCategory, etcash;

    RecyclerView  recyclerView2, recyclerView1;
    RecyclerView.Adapter myAdapter2, myAdapter1;
    RecyclerView.LayoutManager LayoutManager2, LayoutManager1;
    Button btnIncome, btnExpense;
    ArrayList<Budget> money;
    ArrayList<Budget2> money2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvtotal = findViewById(R.id.tvtotal);
        tvtotal2 = findViewById(R.id.tvtotal2);
        tvtotal3 = findViewById(R.id.tvtotal3);

        etCategory = findViewById(R.id.etCategory);
        etcash = findViewById(R.id.etcash);

        recyclerView2 = findViewById(R.id.list2);
        recyclerView2.setHasFixedSize(true);

        recyclerView1 = findViewById(R.id.list);
        recyclerView1.setHasFixedSize(true);

        btnIncome = findViewById(R.id.btnIncome);
        btnExpense = findViewById(R.id.btnExpense);


        btnIncome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                money.add(new Budget( etCategory.getText().toString().trim(), etcash.getText().toString().trim()));
                myAdapter1.notifyDataSetChanged();
                recyclerView1.setAdapter(myAdapter1);

                int totalPrice = 0;
                for (int i = 0; i<money.size(); i++)
                {

                    totalPrice += Integer.parseInt(money.get(i).getNumbers());
                    totalPricefinal = Integer.toString(totalPrice);
                }

                tvtotal.setText(Integer.toString(totalPrice));
            }
        });

        btnExpense.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                money2.add(new Budget2( etCategory.getText().toString().trim(), etcash.getText().toString().trim()));
                myAdapter2.notifyDataSetChanged();
                recyclerView2.setAdapter(myAdapter2);

                int totalPrice2 = 0;
                for (int i = 0; i<money2.size(); i++)
                {
                    totalPrice2 += Integer.parseInt(money2.get(i).getNumbers2());
                }

                tvtotal2.setText(Integer.toString(totalPrice2));

            }
        });

        value1 = Integer.parseInt(tvtotal.getText().toString());
        value2 = Integer.parseInt(tvtotal2.getText().toString());
        value3 = value1-value2;

        tvtotal3.setText(Integer.toString(value3));

        LayoutManager2 = new LinearLayoutManager(this);
        recyclerView2.setLayoutManager(LayoutManager2);

        LayoutManager1 = new LinearLayoutManager(this);
        recyclerView1.setLayoutManager(LayoutManager1);

        money = new ArrayList<Budget>();
        money2 = new ArrayList<Budget2>();

        myAdapter1 = new budgetAdapter(this,money);
        recyclerView1.setAdapter(myAdapter1);

        myAdapter2 = new budgetAdapter2(this,money2);
        recyclerView2.setAdapter(myAdapter2);


    }
}


BudgetAdapter.java

Java
<pre>public class budgetAdapter extends RecyclerView.Adapter<budgetAdapter.ViewHolder> {

    private ArrayList<Budget> money;
    private int finalprice = 0;

    public budgetAdapter (Context context, ArrayList<Budget> List)
    {
        money = List;
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {

        TextView tvName;
        TextView tvNumbers;

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

            tvName = itemView.findViewById(R.id.tvName);
            tvNumbers = itemView.findViewById(R.id.tvNumbers);
        }
    }

    @NonNull
    @Override
    public budgetAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull budgetAdapter.ViewHolder viewHolder, int i) {
        viewHolder.itemView.setTag(money.get(i));
        viewHolder.tvName.setText(money.get(i).getName());
        viewHolder.tvNumbers.setText(money.get(i).getNumbers());

    }

    @Override
    public int getItemCount() {
        return money.size();
    }
}


Budget.java
Java
public class Budget extends MainActivity {
    private String name;
    private String numbers;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumbers() {
        return numbers;
    }

    public void setNumbers(String numbers) {
        this.numbers = numbers;
    }

    public Budget(String name, String numbers) {
        this.name = name;
        this.numbers = numbers;
    }
}


BudgetAdapter2.java
Java
<pre>public class budgetAdapter2 extends RecyclerView.Adapter<budgetAdapter2.ViewHolder> {



    private ArrayList<Budget2> money2;

    public budgetAdapter2 (Context context, ArrayList<Budget2> List)
    {
        money2 = List;
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {

        TextView tvName;
        TextView tvNumbers;

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

            tvName = itemView.findViewById(R.id.tvName);
            tvNumbers = itemView.findViewById(R.id.tvNumbers);
        }
    }


    @NonNull
    @Override
    public budgetAdapter2.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull budgetAdapter2.ViewHolder viewHolder, int i) {

        viewHolder.itemView.setTag(money2.get(i));
        viewHolder.tvName.setText(money2.get(i).getName2());
        viewHolder.tvNumbers.setText(money2.get(i).getNumbers2());

    }

    @Override
    public int getItemCount() {
        return money2.size();
    }
}


Budget2.java

Java
public class Budget2 {
    private String name2;
    private String numbers2;

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public String getNumbers2() {
        return numbers2;
    }

    public void setNumbers2(String numbers2) {
        this.numbers2 = numbers2;
    }

    public Budget2(String name2, String numbers2) {
        this.name2 = name2;
        this.numbers2 = numbers2;
    }
}


What I have tried:

At the last column, i tried to get the data from the for loops. The application manage to compile but no data was shown during testing. Please help.
Posted
Updated 2-Jul-19 6:19am
v3
Comments
wseng 28-Jun-19 10:59am    
can you get value1 and value2?
howtocode101 28-Jun-19 11:26am    
No. I cant get value 1 and 2. Manage to compile but could not the app is force close. Logcat shows highlight starts at value 1 line.
wseng 28-Jun-19 11:28am    
Please post the logcat.
howtocode101 28-Jun-19 20:25pm    
Hi, i have already update my logcat
wseng 28-Jun-19 22:49pm    
Which variable return "Total"? It should return only int, not String.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900