Home > Mobile >  Keep data in ListView after refresh - Android Studio
Keep data in ListView after refresh - Android Studio

Time:03-07

I have a Main Activity and PersonListView Activity. In Main Activity, there are some input text and when click save button, it will send data and save to listview. Well, I have some trouble with keep data in that listview because everytime I return to main activity and comback to listview, the old data is gone. Is there anyway to save data but not use database.

Here is my Main Activity:

public class MainActivity extends AppCompatActivity {

String str, gender, vaccine, date;

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

    Button okay = (Button) findViewById(R.id.btnOk);
    Button list = (Button) findViewById(R.id.btnList);

    EditText name = (EditText) findViewById(R.id.inputName);
    EditText address = (EditText) findViewById(R.id.inputAdd);
    EditText phone = (EditText) findViewById(R.id.inputPhone);

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    RadioButton female = (RadioButton) findViewById(R.id.inputFemale);
    RadioButton male = (RadioButton) findViewById(R.id.inputMale);

    CheckBox first = (CheckBox)findViewById(R.id.inputFirst);
    CheckBox second = (CheckBox)findViewById(R.id.inputSecond);
    CheckBox third = (CheckBox)findViewById(R.id.inputThird);

    EditText datefirst = (EditText) findViewById(R.id.dateFirst);
    EditText datesecond = (EditText) findViewById(R.id.dateSecond);
    EditText datethird = (EditText) findViewById(R.id.dateThird);

    TextView result = (TextView)findViewById(R.id.textResult);


    okay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(female.isChecked()) gender = female.getText().toString();
            if(male.isChecked()) gender = male.getText().toString();
            if(first.isChecked()) {
                vaccine = first.getText().toString();
                date = datefirst.getText().toString();
            }
            if(second.isChecked()) {
                vaccine = second.getText().toString();
                date = datesecond.getText().toString();
            }
            if(third.isChecked()) {
                vaccine = third.getText().toString();
                date = datethird.getText().toString();
            }

            str = name.getText().toString()   "\n"   address.getText().toString()   "\n"   phone.getText().toString()   "\n"  
                    gender   "\n"   vaccine   "\n"   date;


            result.setText(str);
            Toast.makeText(getApplicationContext(),result.getText().toString(),Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(MainActivity.this, PersonView.class);
            intent.putExtra("NAME",name.getText().toString());
            intent.putExtra("ADDRESS",address.getText().toString());
            intent.putExtra("PHONE",phone.getText().toString());
            intent.putExtra("GENDER",gender);
            intent.putExtra("VACCINE",vaccine);
            intent.putExtra("DATE",date);

            startActivity(intent);

        }
    });

And here is ListView Activity:

public class PersonView extends AppCompatActivity {

ArrayList<Person> listPerson;
PersonListViewAdapter personListViewAdapter;
ListView listViewPerson;

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

    Button back = findViewById(R.id.btnBack);

    back.setOnClickListener(view -> {
        Intent intent = new Intent(PersonView.this, MainActivity.class);
        startActivity(intent);
    });

    listPerson = new ArrayList<>();
    listPerson.add(new Person("Lieu Mai","25 Tran Quang Dieu", "0786367073", "female","3 injection", "24/07/2000"));

    personListViewAdapter = new PersonListViewAdapter(listPerson);
    listViewPerson = findViewById(R.id.listPerson);
    listViewPerson.setAdapter(personListViewAdapter);
    updateList();


}

private void updateList()
{
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if(bundle != null)
    {
        Person newPerson = new Person((String)bundle.get("NAME"), (String)bundle.get("ADDRESS"), (String)bundle.get("PHONE"), (String)bundle.get("GENDER"),(String)bundle.get("VACCINE"),(String)bundle.get("DATE"));
        listPerson.add(newPerson);
        personListViewAdapter.notifyDataSetChanged();
    }
}


class Person {
    String name, address, phone, gender, vaccine, date;

    public Person( String name, String address, String phone, String gender, String vaccine, String date) {
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.gender = gender;
        this.vaccine = vaccine;
        this.date = date;
    }
}

class PersonListViewAdapter extends BaseAdapter {

    final ArrayList<Person> listPerson;

    PersonListViewAdapter(ArrayList<Person> listPerson) {
        this.listPerson = listPerson;
    }

    @Override
    public int getCount() {

        return listPerson.size();
    }

    @Override
    public Object getItem(int position) {

        return listPerson.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View viewPerson;
        if (convertView == null) {
            viewPerson = View.inflate(parent.getContext(), R.layout.person_view, null);
        } else viewPerson = convertView;


        Person person = (Person) getItem(position);
        ((TextView) viewPerson.findViewById(R.id.txtName)).setText(String.format("Name: %s", person.name));
        ((TextView) viewPerson.findViewById(R.id.txtAddress)).setText(String.format("Address : %s", person.address));
        ((TextView) viewPerson.findViewById(R.id.txtPhone)).setText(String.format("Phone number: %s", person.phone));
        ((TextView) viewPerson.findViewById(R.id.txtGender)).setText(String.format("Gender: %s", person.gender));
        ((TextView) viewPerson.findViewById(R.id.txtVaccine)).setText(String.format("Vaccine: %s", person.vaccine));
        ((TextView) viewPerson.findViewById(R.id.txtDate)).setText(String.format("Date: %s", person.date));

        return viewPerson;
    }
}

}

CodePudding user response:

You can save listPerson in shared preferences and retrieve it on oncreate method in PersonView Activity.

CodePudding user response:

You can use viewModel and attach it to your activity to save your list and list will be alive till the instance of your app is alive. Or you can create a companion object list, companion object also remains alive till instance of app is alive.

But if you want to permanently store your data either you have to use a Database or sharedPrefrences to save your values permanently.

  • Related