java - How do I add to ListView and not replace it? -
i'm new android , i'm trying add things listview activity. able add things list, each time add list replaces entered.
i have tried using notifydatasetchanged() invalidateviews() neither seem work?
here related files:
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=".mainactivity" android:weightsum="1" android:orientation="vertical"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add_button" android:id="@+id/add_button" android:layout_gravity="bottom" android:onclick="ongetrecipeclick"/> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/recipe_name_list" android:layout_margintop="20dp" android:background="#000000" /> </linearlayout>
second_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp"> <edittext android:layout_width="250dp" android:layout_height="wrap_content" android:id="@+id/recipe_name_edit_text"/> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/enter_recipe_button_text" android:id="@+id/save_button" android:onclick="onsendrecipename"/> </linearlayout>
mainactivity.java
package com.jamielammas.recipeexample; import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; public class mainactivity extends actionbaractivity { public static listview listview; public static arrayadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void ongetrecipeclick(view view) { intent getrecipescreenintent; getrecipescreenintent = new intent(this, secondscreen.class); final int result = 1; getrecipescreenintent.putextra("callingactivity", "mainactivity"); startactivityforresult(getrecipescreenintent, result); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); listview = (listview) findviewbyid(r.id.recipe_name_list); adapter = new arrayadapter<>(getapplicationcontext(), android.r.layout.simple_list_item_1, secondscreen.stringarray); listview.setadapter(adapter); adapter.notifydatasetchanged(); } }
secondscreen.java
package com.jamielammas.recipeexample; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.edittext; import java.util.arraylist; public class secondscreen extends activity { public static edittext edittext; public static arraylist<string> stringarray; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.second_layout); } public void onsendrecipename(view view) { edittext = (edittext) findviewbyid(r.id.recipe_name_edit_text); stringarray = new arraylist<>(); stringarray.add(secondscreen.edittext.gettext().tostring()); intent goingback = new intent(); setresult(result_ok, goingback); finish(); } }
any appreciated!
you can this:
secondscreen
public void onsendrecipename(view view) { edittext = (edittext) findviewbyid(r.id.recipe_name_edit_text); intent goingback = new intent(); goingback.putextra("newitem",edittext.gettext().tostring()); setresult(result_ok, goingback); finish(); }
mainactivity
arraylist<string> arrayusedtocreatetheadapter = new arraylist<>(); @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == result_ok && data.hasextra("newitem")){ arrayusedtocreatetheadapter.add(data.getstringextra("newitem");); adapter.notifydatasetchanged(); } }