'Saving the results from editText into a List in object class
I am trying to solve a problem I have been stuck on for a while. I am trying to save the results from a user submit to a list. I am able to do that but when I leave to a previous oage in the app and come back to the page all of the data is gone. I am working in android studios. I have a User object class that I am storing the data for indivisual users of the app below. Can someone help me with how I can save the results of the user input in a list of some sorts.
This is my User.java File
package com.example.fitnessapplication;
import java.util.Collections;
import java.util.List;
public class User {
private String fullName, age, email;
private String gender, address;
public User() {
}
public User(String fullName, String age, String email)
{
this.fullName = fullName;
this.age = age;
this.email = email;
gender = "";
address = "";
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
this is my main Goals.java
file
package com.example.fitnessapplication;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class Goals extends AppCompatActivity{
List<String> toDoList;
ArrayAdapter<String> arrayAdapter;
ListView listView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goals);
toDoList = new ArrayList<>();
arrayAdapter = new ArrayAdapter<>(this,R.layout.list_view_layout, toDoList);
listView = findViewById(R.id.id_list_view);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView = (TextView) view;
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
});
editText = findViewById(R.id.id_edit_text);
}
public void addItemToList(View view){
toDoList.add(editText.getText().toString());
arrayAdapter.notifyDataSetChanged();
editText.setText("");
}
}
this is my xml activity_goals.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Goals" >
<LinearLayout
android:id="@+id/id_bottom_selection"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<EditText
android:id="@+id/id_edit_text"
android:layout_width="0dp"
android:layout_weight = "1"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"/>
<Button
android:onClick="addItemToList"
android:layout_width="wrap_content"
android:text="Add"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_above="@+id/id_bottom_selection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee">
<ListView
android:id="@+id/id_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
I am expecting whenever I make a new text view for the goal setting portion for it to still be there once I leave and come back to the page.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
