java - Why isn't my listview updating? -
i've got problem listview. when press button should add item listview, doesn't. i've used toast sure onclick event button good. can me ? fragment's java code.
package com.wordpress.softwarebycs.i_cseditor; import android.app.actionbar; import android.app.activity; import android.content.context; import android.graphics.color; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.text.layout; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.webkit.webview; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.linearlayout; import android.widget.listview; import android.widget.spinner; import android.widget.tabhost; import android.widget.tabwidget; import android.widget.textview; import android.widget.toast; import java.io.bufferedreader; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.util.arraylist; /** * created cosmin on 11.04.2015. */ public class code_fragment extends fragment implements adapterview.onitemselectedlistener{ view rootview; webview previewbrowser; edittext codebox,fname; button save_btn; spinner spinner; arraylist<string> list = new arraylist<string>(); arrayadapter<string> list_adapter; listview projects; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstance) { rootview = inflater.inflate(r.layout.code_layout,container, false); previewbrowser = (webview)rootview.findviewbyid(r.id.previewbrowser); codebox= (edittext)rootview.findviewbyid(r.id.codebox); projects=(listview)rootview.findviewbyid(r.id.projectslistview); list_adapter=new arrayadapter<string>(rootview.getcontext(), android.r.layout.simple_list_item_1, list); projects.setadapter(list_adapter); list.clear(); save_btn=(button)rootview.findviewbyid(r.id.save_btn); save_btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { writefile(); } }); fname=(edittext)rootview.findviewbyid(r.id.fname); spinner= (spinner)rootview.findviewbyid(r.id.spinner); arrayadapter adapter = arrayadapter.createfromresource(rootview.getcontext(),r.array.spinner_items,android.r.layout.simple_spinner_dropdown_item); spinner.setadapter(adapter); final tabhost tabhost=(tabhost)rootview.findviewbyid(r.id.tabhost); tabhost.setup(); tabhost.setontabchangedlistener(new tabhost.ontabchangelistener() { @override public void ontabchanged(string tabid) { string file="i_c_s_editor_temp_file_ne_trebuie_un_nume_lung_tare_de_tot.html"; try { fileoutputstream fos = getactivity().openfileoutput(file, context.mode_private); fos.write(codebox.gettext().tostring().getbytes()); fos.close(); } catch (exception e) { e.printstacktrace(); } previewbrowser.loadurl("file:///data/data/com.wordpress.softwarebycs.i_cseditor/files/"+file); } }); tabhost.tabspec spec1=tabhost.newtabspec("code"); spec1.setcontent(r.id.codetab); spec1.setindicator("code"); tabhost.tabspec spec2=tabhost.newtabspec("preview"); spec2.setcontent(r.id.previewtab); spec2.setindicator("preview"); tabhost.tabspec spec3=tabhost.newtabspec("file"); spec3.setcontent(r.id.filetab); spec3.setindicator("file"); tabhost.addtab(spec1); tabhost.addtab(spec2); tabhost.addtab(spec3); for(int i=0;i<tabhost.gettabwidget().getchildcount();i++) { textview tv = (textview) tabhost.gettabwidget().getchildat(i).findviewbyid(android.r.id.title); tv.settextcolor(color.parsecolor("#ffe0e0e0")); } projects.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parentadapter, view view, int position, long id) { textview clickedview = (textview) view; readfile(clickedview.gettext().tostring()); //tabhost.setcurrenttab(0); } }); return rootview; } public void writefile() { string file_name=fname.gettext().tostring()+spinner.getselecteditem().tostring(); try { fileoutputstream fos = getactivity().openfileoutput(file_name, context.mode_private); fos.write(codebox.gettext().tostring().getbytes()); list.add(file_name); toast toast = toast.maketext(rootview.getcontext(), "file saved!", toast.length_short); toast.show(); fos.close(); } catch (exception e) { e.printstacktrace(); } } @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { } @override public void onnothingselected(adapterview<?> parent) { } public void actionstopped(){ try { fileoutputstream fos = getactivity().openfileoutput("cfg2.txt", context.mode_private); integer k; for(k=0;k<list.size();k++) {fos.write(list.get(k).tostring().getbytes()); fos.write(system.getproperty("line.separator").getbytes());} fos.close(); } catch (exception e) { e.printstacktrace(); } } public void actionresummed() { list.clear(); try { bufferedreader breader = new bufferedreader(new inputstreamreader(getactivity().openfileinput("cfg2.txt"))); string line; while ((line = breader.readline()) != null) { list.add(line); } } catch (ioexception e) { e.printstacktrace(); } } @override public void onresume() { super.onresume(); actionresummed(); } @override public void onpause() { super.onpause(); actionstopped(); } public void readfile(string path) { codebox.settext(""); try { bufferedreader breader = new bufferedreader(new inputstreamreader(getactivity().openfileinput(path))); string line; while ((line = breader.readline()) != null) { codebox.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } } }
and fragment's xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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:background="#ff666666"> <tabhost android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tabhost" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true"> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ff8a3e6b"> <tabwidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"></tabwidget> <framelayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:id="@+id/codetab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <edittext android:layout_width="fill_parent" android:layout_height="fill_parent" android:inputtype="textmultiline" android:ems="10" android:id="@+id/codebox" android:gravity="top|left" android:background="#ffffffff" android:scrollbars="vertical|horizontal"/> </linearlayout> <linearlayout android:id="@+id/previewtab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <webview android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/previewbrowser" android:scrollbars="vertical|horizontal" ></webview> </linearlayout> <!--filetab--> <linearlayout android:id="@+id/filetab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff666666" android:orientation="vertical" > <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearlayout" android:layout_marginbottom="20dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="file's name" android:id="@+id/textview2" android:layout_gravity="center_horizontal" android:textcolor="#ffe0e0e0" android:layout_below="@+id/linearlayout" android:layout_centerhorizontal="true" android:layout_margintop="20dp" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout" android:layout_centerhorizontal="true" android:id="@+id/linearlayout2"> <edittext android:layout_width="237dp" android:layout_height="41dp" android:id="@+id/fname" android:layout_margintop="10dp" android:background="#ff8a3e6b" android:textcolor="#ffe0e0e0" /> <spinner android:layout_width="fill_parent" android:layout_height="41dp" android:id="@+id/spinner" android:background="#ff8a3e6b" android:layout_margintop="9dp" /> </linearlayout> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout2" android:layout_centerhorizontal="true" android:id="@+id/linearlayout3"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="save file" android:id="@+id/save_btn" android:layout_gravity="center" android:layout_margintop="10dp" android:background="#ff8a3e6b" android:textcolor="#ffe0e0e0" /> </linearlayout> <!-- separator--> <textview android:layout_width="fill_parent" android:layout_height="1dp" android:id="@+id/separator" android:background="#ff8a3e6b" android:layout_margintop="5dp" /> <!--open file --> <linearlayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/separator" android:layout_centerhorizontal="true" android:id="@+id/linearlayout4"> <textview android:paddingtop="20dp" android:paddingbottom="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text2" android:textsize="25dp" android:text="select file open!" android:layout_gravity="top|center" /> <listview android:layout_width="fill_parent" android:layout_below="@+id/text2" android:layout_height="fill_parent" android:id="@+id/projectslistview" android:scrollbars="vertical|horizontal" android:background="#ff8a3e6b"> </listview> </linearlayout> </linearlayout> </framelayout> </linearlayout> </tabhost> </relativelayout>
to update listview have notify it, don't see notifying method in click method
add below code in
save_btn.setonclicklistener //method after calling writefile() method list_adapter.notifydatasetchanged();
Comments
Post a Comment