android - Not adding items to list with SharedPreferences? -
i'm trying make app can add item list using edittext , listview. has remember added list because there multiple activities, i'm using sharedpreferences. first time activity opened crashed because looked 1 of sharedpreferences files wasn't there, added
if (newquestion == null) { return; } but never adds list.
my full code activity:
package com.example.dogwise; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import java.util.arraylist; import java.util.arrays; import java.util.list; import android.widget.arrayadapter; import android.widget.listview; import android.widget.adapterview; import android.content.sharedpreferences; public class questions extends appcompatactivity { listview listview; string[] listelements = new string[]{ "how food should feed dog?", "how teach dog sit?" }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_questions); listview = (listview) findviewbyid(r.id.questionslist); final list<string> listelementsarraylist = new arraylist<string>(arrays.aslist(listelements)); final arrayadapter<string> adapter = new arrayadapter<string> (questions.this, android.r.layout.simple_list_item_1, listelementsarraylist); listview.setadapter(adapter); adapter.notifydatasetchanged(); sharedpreferences pref = getapplicationcontext().getsharedpreferences("mypref", mode_private); string newquestion=pref.getstring("key_question", null); if (newquestion == null) { return; } listelementsarraylist.add(newquestion); adapter.notifydatasetchanged(); bundle newquestion = getintent().getextras(); if (newquestion == null) { return; } final string questionname = newquestion.getstring("questionname"); listelementsarraylist.add(questionname); adapter.notifydatasetchanged(); sharedpreferences.editor editor = getsharedpreferences("mypref", mode_private).edit(); editor.putstring("key_question", questionname); editor.apply(); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == 0) { intent myintent = new intent(view.getcontext(), question_1.class); startactivityforresult(myintent, 0); } if (position == 1) { intent myintent = new intent(view.getcontext(), question_2.class); startactivityforresult(myintent, 0 ); } if (position == 2) { intent myintent = new intent(view.getcontext(), question_asked.class); myintent.putextra("questiontitle", questionname); startactivityforresult(myintent, 0); } } }); } ; public void backhomeonclick(view view) { intent b = new intent(this, homescreen.class); startactivity(b); } public void askaquestiononclick(view view) { intent = new intent(this, askaquestion.class); startactivity(i); } ; }
you publishing data intent using key= "questiontitle" trying retrieve using key= "questionname".
Comments
Post a Comment