vendredi 17 juin 2016

How to save data in a listView

I created a listView that is based on the user input. User has to enter text in an editText and then it's added into the list. It works great. My question though, is how do I save this data so even when they close the app, and then open it back up again, the listView still has the items in it from last time? Here is my code:

MainActivity.java

    package com.kass.planner2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener {

    private Button btn;
    private EditText et;
    private ListView lv;
    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(this);
        et = (EditText)findViewById(R.id.editText);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

        // set the lv variable to your list in the xml
        lv=(ListView)findViewById(R.id.listView);
        lv.setAdapter(adapter);
    }
    public void onClick(View v)
    {
        String input = et.getText().toString();
        if(input.length() > 0)
        {
            // add string to the adapter, not the listview
            adapter.add(input);
            // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
        }
    }


}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.kass.planner2.MainActivity"
    android:weightSum="1">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_weight="1.12">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="60dp"
            android:layout_alignEnd="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true"
            android:onClick="saveEvent"/>

        <ListView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:id="@+id/listView"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true"
            android:layout_toEndOf="@+id/editText" />
    </RelativeLayout>


</LinearLayout>

Thank you.

Aucun commentaire:

Enregistrer un commentaire