0

i have one list view with two text views inside it, one edit text that is in the same activity but not in the list view and two buttons one to add to the list view and the other to delete from it. how to add integers to the first text view, the sum of all integers to the second one, and to be from a custom adapter. thank you.

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=".MainActivity"
    android:orientation="vertical">
    <EditText
        android:id="@+id/edit_ten"
        android:hint="Score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_add"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Add"/>
        <Button
            android:id="@+id/btn_delete"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Undo"/>

    </LinearLayout>
    <ListView
        android:id="@+id/list_sinhvien"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</LinearLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <LinearLayout
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_ten"
            android:textSize="20dp"
            android:text="Score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/text_sdt"
            android:textSize="20dp"
            android:text="Total"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
MainActivity.java
package com.example.addanddelete;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ListView listSinhvien;
    EditText editTen;
    Button btnThem , btnSua;
    ArrayList<Sinhvien> arraySinhvien;
    CustomAdapter myadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        anhxa();
        arraySinhvien = new ArrayList<Sinhvien>();


        myadapter = new CustomAdapter(this , R.layout.item_layout,arraySinhvien);
        listSinhvien.setAdapter(myadapter);
        btnSua.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final int count = myadapter.getCount();
                myadapter.remove(myadapter.getItem(count -1));
                myadapter.notifyDataSetChanged();

                return;}});

    }
    private void anhxa(){
        listSinhvien = (ListView)findViewById(R.id.list_sinhvien);
        editTen = (EditText)findViewById(R.id.edit_ten);
        btnThem = (Button)findViewById(R.id.btn_add);
        btnSua = (Button)findViewById(R.id.btn_undo);
        btnThem.setOnClickListener(this);
        btnSua.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add:
                Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
                String ten = editTen.getText().toString();
                String sdt = editTen.getText().toString();
                Sinhvien temp = new Sinhvien(R.mipmap.ic_launcher,ten , sdt);
                arraySinhvien.add(temp);
                myadapter.notifyDataSetChanged();
                break;
        }

    }
}
CustomAdapter.java
package com.example.addanddelete;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class CustomAdapter extends ArrayAdapter {
    Activity activity;
    int layout;
    ArrayList<Sinhvien> arrSinhVien;

    public CustomAdapter(@NonNull  Activity activity, int layout, @NonNull ArrayList<Sinhvien> arrSinhVien) {
        super(activity, layout, arrSinhVien);
        this.activity = activity;
        this.layout = layout;
        this.arrSinhVien = arrSinhVien;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater layoutInflater = activity.getLayoutInflater();
        convertView = layoutInflater.inflate(layout, null);
        TextView ten = (TextView) convertView.findViewById(R.id.text_score);
        TextView sdt = (TextView) convertView.findViewById(R.id.text_total);

        ten.setText(arrSinhVien.get(position).getTenSinhvien());
        sdt.setText(arrSinhVien.get(position).getSdtSinhvien());
        return convertView;
    }
}
Sinhvien.java
package com.example.addanddelete;

public class Sinhvien {

    String tenSinhvien;
    String sdtSinhvien;

    public Sinhvien(String iclauncher,String ten, String sdt) {
    }

    public Sinhvien(int iclauncher,String tenSinhvien, String sdtSinhvien) {

        this.tenSinhvien = tenSinhvien;
        this.sdtSinhvien = sdtSinhvien;
    }


    public String getTenSinhvien() {
        return tenSinhvien;
    }

    public void setTenSinhvien(String tenSinhvien) {
        this.tenSinhvien = tenSinhvien;
    }

    public String getSdtSinhvien() {
        return sdtSinhvien;
    }

    public void setSdtSinhvien(String sdtSinhvien) {
        this.sdtSinhvien = sdtSinhvien;
    }
}

Ahmed Zaqout
  • 91
  • 1
  • 11

1 Answers1

0

The general behaviour/implementation that you've outlined in your question is very well documented. That said, I'd suggest considering utilising a RecyclerView with associated custom item view and adapter, as opposed to a ListView. There are a few reasons why I'd suggest this.

I did a little searching and found several examples that cover the general idea of your implementation. This example does a great job of illustrating how to achieve what you're seeking. The article begins by outlining some reasons to work with a RecyclerView over a ListView or GridView, then proceeds to give an in-depth run-down on how to implement a RecyclerView with custom adapter (and associated item view and item class).

At a glance, your implementation would require:

  • An Activity containing your RecyclerView, two Buttons (used for adding and deleting elements from the RecyclerView) and an EditText for taking user input.
  • A custom item View representing individual items of your RecyclerView list. This would contain the two TextView views (one for displaying the integer and the other for displaying the sum of all integers).
  • A custom item model Class to represent the data model for the above custom item View. This would hold an integer value and likely some logic for displaying the sum.
  • A custom RecyclerView adapter (which ties all of the above together). This will need to handle the task of binding data from your dataset (that grows and shrinks based on user input) to instances of your custom items that are to appear in the RecyclerView list. This adapter could also be used by your add and delete item buttons to modify the elements in the RecyclerView list.

The above is outlined in far greater depth in the link I provided earlier.

I sincerely hope that helps!

Tom Larcher
  • 661
  • 9
  • 24