inputData to send data to mainActivity error using Realm Database instant crash

Viewed 15

I have a problem that when I press the Fab button the app instant close and I don't know how to fix it, my goal was when pressing the fab button and inserting the data it will send the data to the MainActivity.xml and make a list of item

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Income"
                android:textSize="24dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="0"
                android:textSize="20dp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Expenses"
                android:textSize="24dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="0"
                android:textSize="20dp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Balance"
                android:textSize="24dp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="0"
                android:textSize="20dp" />


        </LinearLayout>


    </LinearLayout>

    <ListView
        android:id="@+id/listView"
        app:layout_anchor="@id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="80dp">
    </ListView>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:fabCradleMargin="10dp"
        app:fabCradleVerticalOffset="10dp"
        app:fabCradleRoundedCornerRadius="20dp"
        android:layout_gravity="bottom">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/bottomNavigationView"
            app:menu="@menu/bottom_nav_menu"
            android:layout_marginEnd="10dp"/>
    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fab"
        android:src="@drawable/add_button"
        app:layout_anchor="@id/bottomAppBar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

activity_input_data.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"
    android:orientation="vertical"
    tools:context=".InputDataActivity">


    <LinearLayout
        android:layout_marginTop="200dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginBottom="200dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView

            android:textAlignment="center"
            android:textSize="50dp"
            android:textStyle="bold"
            android:text="Input Data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/txtTitle"
            android:hint="Title..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/txtAmount"
            android:hint="Amount..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/txtDate"
            android:hint="Date..."
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@+id/addButton"
            android:text="Add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>


</LinearLayout>

list_item.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="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">
    
    <ImageView
        android:src="@drawable/profile_button"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title"
            android:layout_gravity="end"
            android:textSize="16dp"
            android:textStyle="bold"
            android:text="Main Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/amount"
            android:layout_gravity="end"
            android:textSize="14dp"
            android:textStyle="bold"
            android:text="Rp. 20.000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/date"
            android:layout_gravity="end"
            android:textSize="12dp"
            android:textStyle="bold"
            android:text="23-06-2021"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/spinner"
            android:layout_gravity="end"
            android:textSize="14dp"
            android:textStyle="bold"
            android:text="Expense"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ListView listView;
    MyHelper myHelper;
    BottomNavigationView bottomNavigationView;
    FloatingActionButton floatingActionButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        floatingActionButton = findViewById(R.id.fab);

        //ListView Adapter
        ListViewAdapter adapter = new ListViewAdapter(this, myHelper.justRefresh());
        listView.setAdapter(adapter);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, InputDataActivity.class);
                startActivity(intent);
            }
        });

        //Bottom Nav
        bottomNavigationView.setBackground(null);
        bottomNavigationView.getMenu().getItem(2).setEnabled(false);
    }
}

InputDataActivity.java

public class InputDataActivity extends AppCompatActivity {

    EditText txtTitle, txtAmount,txtDate;
    Button button;
    Realm realm;
    ListView listView;
    MyHelper myHelper;
    RealmChangeListener realmChangeListener;

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

        realm = Realm.getDefaultInstance();

        txtTitle = findViewById(R.id.txtTitle);
        txtAmount = findViewById(R.id.txtAmount);
        txtDate = findViewById(R.id.txtDate);
        button = findViewById(R.id.addButton);
        listView = findViewById(R.id.listView);

        //MyHelper
        myHelper = new MyHelper(realm);
        myHelper.selectFromDB();

        //ListView Adapter
        ListViewAdapter adapter = new ListViewAdapter(this, myHelper.justRefresh());
        listView.setAdapter(adapter);

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

        Refresh();

    }

    private void saveData(){
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                Number maxId = realm.where(User.class).max("title_id");

                int newKey = (maxId == null) ? 1 : maxId.intValue()+1;

                User user = realm.createObject(User.class, newKey);
                user.setTitle_name(txtTitle.getText().toString());
                user.setAmount(txtAmount.getText().toString());
                user.setDate(txtDate.getText().toString());

            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Toast.makeText(InputDataActivity.this, "Success", Toast.LENGTH_SHORT).show();
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Toast.makeText(InputDataActivity.this, "Fail", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void Refresh(){
        realmChangeListener = new RealmChangeListener() {
            @Override
            public void onChange(Object o) {
                ListViewAdapter adapter = new ListViewAdapter(InputDataActivity.this, myHelper.justRefresh());
                listView.setAdapter(adapter);
            }
        };
        realm.addChangeListener(realmChangeListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.removeChangeListener(realmChangeListener);
        realm.close();
    }
}

MyHelper.java

public class MyHelper {

    Realm realm;
    RealmResults<User> users;

    public MyHelper(Realm realm) {
        this.realm = realm;
    }

    public void selectFromDB(){
        users = realm.where(User.class).findAll();
    }

    public ArrayList<User> justRefresh(){
        ArrayList<User> listItem = new ArrayList<>();
        for(User user: users){
            listItem.add(user);
        }

        return listItem;
    }
}

User.java

public class User extends RealmObject {

    @PrimaryKey
    private int title_id;

    private String title_name;
    private String amount;
    private String date;
    private String spinner;

    public int getTitle_id() {
        return title_id;
    }

    public void setTitle_id(int title_id) {
        this.title_id = title_id;
    }

    public String getTitle_name() {
        return title_name;
    }

    public void setTitle_name(String title_name) {
        this.title_name = title_name;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSpinner() {
        return spinner;
    }

    public void setSpinner(String spinner) {
        this.spinner = spinner;
    }
}

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

    Context context;
    ArrayList<User> users;

    public ListViewAdapter(Context context, ArrayList<User> users) {
        this.context = context;
        this.users = users;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        view =inflater.inflate(R.layout.list_item, parent,false);

        TextView title,amount,date,spinner;

        title =view.findViewById(R.id.title);
        amount = view.findViewById(R.id.amount);
        date = view.findViewById(R.id.date);
        spinner = view.findViewById(R.id.spinner);

        User u = (User)this.getItem(position);

        title.setText(u.getTitle_name());
        amount.setText(u.getAmount());
        date.setText(u.getDate());
        spinner.setText(u.getSpinner());

        int numPosition = u.getTitle_id();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // for update
            }
        });
        return view;
    }
}

if someone can help, Thank you because I'm stuck here. my goal is to make Money Management

0 Answers
Related