listview - Android - Clear Values after AlertDialog is Dismissed -
i'm making app contains listview
, long-clicking of item of listview
pop-up alertdialog
custom view
offer user further options.now, there textview
in custom view
display name of item, has been long-clicked user. but, problem occurs when long-click item in listview
second time.
let me explain example -
suppose, there 4 items in list -
- english
- maths
- physics
- geography
let's user long-clicked on "maths" after activity
loads(first time). result, alertdialog
pops-up textview
saying "english".
now, after user dismisses previous dialog , again long-clicks on, say, "geography". result, alertdialog
pops-up again textview
saying "english"! whereas, should show "geography".
this code i'm using -
add_topics.java
:
package com.swap.studybuddy; import java.util.arraylist; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.app.alertdialog.builder; import android.app.dialog; import android.content.context; import android.content.dialoginterface; import android.graphics.typeface; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.animation.animation; import android.view.animation.animationutils; import android.view.inputmethod.inputmethodmanager; import android.widget.adapterview; import android.widget.adapterview.onitemlongclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class add_topics extends activity { addtopics_menuadapter adapter; listview topics_list; arraylist<topic> the_subjects = new arraylist<topic>(); string new_subject; subjectsdatabase sd = new subjectsdatabase(this); edittext tpc_nm; inputmethodmanager imm; imageview list_bg; imageview bg_shadow; textview itemtitle; int whereargs2; view dialog; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add__topics); topics_list = (listview) findviewbyid(r.id.topics_list); topics_list.setonitemlongclicklistener(new onitemlongclicklistener() { public boolean onitemlongclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { whereargs2 = arg2; showdialog(1); return false; } }); sd.getwritabledatabase(); the_subjects = sd.gettopics(); overridependingtransition(r.anim.reverse_in_left, r.anim.reverse_out_left); animation enter = animationutils.loadanimation(getbasecontext(), r.anim.upcoming_menu); animation enter_slow = animationutils.loadanimation(getbasecontext(), r.anim.enter_l2r_slide); textview des = (textview)findviewbyid(r.id.des_at); textview title = (textview)findviewbyid(r.id.title_at); button add_topic = (button)findviewbyid(r.id.add_topic_button); typeface roboto_lt = typeface.createfromasset(getassets(), "roboto-light.ttf"); des.settypeface(roboto_lt); title.settypeface(roboto_lt); add_topic.settypeface(roboto_lt); title.startanimation(enter); des.startanimation(enter_slow); adapter = new addtopics_menuadapter(this, the_subjects); topics_list.setadapter(adapter); backgroundchanges(); } public void onclickaddtopic(view v) { showdialog(0); tpc_nm.requestfocus(); getbasecontext(); imm = (inputmethodmanager) getsystemservice(context.input_method_service); imm.togglesoftinput(inputmethodmanager.show_forced, 0); } protected dialog oncreatedialog(int id) { switch(id) { case 0: tpc_nm = new edittext(this); tpc_nm.sethint("new topic/subject name"); typeface roboto_lt = typeface.createfromasset(getassets(), "roboto-light.ttf"); tpc_nm.settypeface(roboto_lt); builder bld = new alertdialog.builder(this); bld.seticon(r.drawable.ic_launcher); bld.settitle("add topic/subject"); bld.setview(tpc_nm); bld.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbuton) { string new_topic_name = tpc_nm.gettext().tostring(); new_subject = new_topic_name; adapter = null; the_subjects.add(new topic(new_topic_name)); sd.addtopic(new topic(new_topic_name)); sd.close(); adapter = new addtopics_menuadapter(getbasecontext(), the_subjects); imm.togglesoftinput(inputmethodmanager.result_hidden, 0); tpc_nm.settext(""); backgroundchanges(); adapter.notifydatasetchanged(); } }); bld.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { imm.togglesoftinput(inputmethodmanager.result_hidden, 0); tpc_nm.settext(""); backgroundchanges(); } }); return bld.create(); case 1: string item = the_subjects.get(whereargs2).gettopic(); final builder build = new alertdialog.builder(this); layoutinflater li = this.getlayoutinflater(); dialog = li.inflate(r.layout.topics_dialog, null); itemtitle = (textview) dialog.findviewbyid(r.id.itemtitle); itemtitle.settext(item); toast.maketext(getbasecontext(), string.valueof(whereargs2) , toast.length_short).show(); build.setview(dialog); return build.create(); case 2: final edittext edited_topic_name = new edittext(this); typeface roboto_light = typeface.createfromasset(getassets(), "roboto-light.ttf"); edited_topic_name.settypeface(roboto_light); edited_topic_name.sethint("new topic name"); builder bld1 = new alertdialog.builder(this); bld1.setview(edited_topic_name); bld1.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface arg0, int arg1) { sd.getwritabledatabase(); sd.updatetopic(new topic(edited_topic_name.gettext().tostring())); sd.close(); the_subjects.clear(); adapter = null; the_subjects = sd.gettopics(); adapter = new addtopics_menuadapter(getbasecontext(), the_subjects); edited_topic_name.settext(""); adapter.notifydatasetchanged(); } }); bld1.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface arg0, int arg1) { edited_topic_name.settext(""); } }); } return null; } /*public void onpause() { super.onpause(); the_subjects.clear(); } public void onstop() { super.onstop(); the_subjects.clear(); } public void ondestroy() { super.ondestroy(); the_subjects.clear(); } */ public void backgroundchanges() { list_bg = (imageview) findviewbyid(r.id.list_bg); bg_shadow = (imageview) findviewbyid(r.id.bg_shadow); int topic_list_length = the_subjects.size(); switch(topic_list_length) { case 0: break; default: list_bg.setimageresource(r.drawable.round); bg_shadow.setimageresource(r.drawable.shadow); } } public void onclickremove(view v) { sd.getwritabledatabase(); sd.removetopic(new topic(the_subjects.get(whereargs2).tostring())); adapter = null; the_subjects = null; the_subjects = sd.gettopics(); sd.close(); adapter = new addtopics_menuadapter(this, the_subjects); dismissdialog(1); adapter.notifydatasetchanged(); topics_list.setadapter(adapter); } public void onclickedit(view v) { showdialog(2); } @override public void onbackpressed() { super.onbackpressed(); overridependingtransition(r.anim.slide_in_left, r.anim.slide_out_left); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_add__topics, menu); return true; } }
addtopics_menuadapter.java
:
package com.swap.studybuddy; import java.util.arraylist; import android.content.context; import android.graphics.typeface; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.animation.animation; import android.view.animation.animationutils; import android.widget.arrayadapter; import android.widget.textview; public class addtopics_menuadapter extends arrayadapter<topic> { private final context context; final arraylist<topic> topics; public addtopics_menuadapter(context context, arraylist<topic> topics) { super(context, r.layout.add_topics_menu, topics); this.context = context; this.topics = topics; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view topicsview = inflater.inflate(r.layout.add_topics_menu, parent, false); final textview topic_name = (textview) topicsview.findviewbyid(r.id.topic_title); typeface rbt_lt = typeface.createfromasset(getcontext().getassets(), "roboto-light.ttf"); animation enter = animationutils.loadanimation(getcontext(), r.anim.upcoming_menu_fast); topic topic = topics.get(position); topic_name.settext(topic.gettopic()); topic_name.settypeface(rbt_lt); topic_name.setanimation(enter); return topicsview; } }
subjectsdatabase.java
:
package com.swap.studybuddy; import java.util.arraylist; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class subjectsdatabase extends sqliteopenhelper{ private static final string subject_id = "id"; private static final string subject_name = "name"; private static final string database_name = "topicsdatabase"; private static final string table_topics = "topics"; private static final int database_version = 1; private static final string database_create = "create_table" + table_topics + "(" + subject_id + "integer_primary_key, " + subject_name + "text" + ")"; public subjectsdatabase(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { try { db.execsql(database_create); } catch(sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exsists topics"); oncreate(db); } public void addtopic(topic topic) { sqlitedatabase db = this.getwritabledatabase(); contentvalues topics = new contentvalues(); topics.put(subject_name, topic.gettopic()); db.insert(table_topics, null, topics); db.close(); } public void removetopic(topic topic) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_topics, subject_id + " = ?", new string[] {string.valueof(topic.get_id())}); db.close(); } public arraylist<topic> gettopics(){ arraylist<topic> topics = new arraylist<topic>(); string selectquery = "select * " + table_topics; sqlitedatabase db = this.getwritabledatabase(); cursor cur = db.rawquery(selectquery, null); if(cur.movetofirst()) { { topic topic = new topic(); topic.set_id(integer.parseint(cur.getstring(0))); topic.settopic(cur.getstring(1)); topics.add(topic); } while(cur.movetonext()); } db.close(); return topics; } public void updatetopic(topic topic_name) { sqlitedatabase db = this.getwritabledatabase(); contentvalues topic = new contentvalues(); topic.put(subject_name, topic_name.gettopic()); db.update(table_topics, topic, subject_id + " = ?", new string[] {string.valueof(topic_name.get_id())}); db.close(); } }
topic.java
package com.swap.studybuddy; public class topic { string topic; int _id; public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public topic() { } public topic(int id, string topic) { this._id = id; this.topic = topic; } public topic(string topic) { this.topic = topic; } public string gettopic() { return topic; } public void settopic(string topic) { topic = topic; } }
please me solve problem. in advance!!
oncreatedialog creates , caches dialogs. second call showdialog re-showing original cached dialog. need override onpreparedialog if want modify dialog contents before displaying.
Comments
Post a Comment