Android: Expandable List/Gridview causing never-ending loading -
my app contains 2 expandableheightlistviews , 1 expandableheightgridview shared vertical scrolling.
the contents of views read in file. works fine small files, data displayed correctly , activity loads.
however, when selecting large files, activity never loads, when debug it, adapters 3 views looping round endlessly. i'm assuming due expanded nature of views trying load everything, surely can avoided?
here code ...
expandableheightlistview ...
public class expandableheightlistview extends listview { boolean expanded = false; public expandableheightlistview(context context) { super(context); } public expandableheightlistview(context context, attributeset attrs) { super(context, attrs); } public expandableheightlistview(context context, attributeset attrs, int defaultstyle) { super(context, attrs, defaultstyle); } public boolean isexpanded() { return expanded; } @override public void onmeasure(int widthmeasurespec, int heightmeasurespec) { // hack! take android! if (isexpanded()) { // calculate entire height providing large height hint. // view.measured_size_mask represents largest height possible. int expandspec = measurespec.makemeasurespec(measured_size_mask, measurespec.at_most); super.onmeasure(widthmeasurespec, expandspec); viewgroup.layoutparams params = getlayoutparams(); params.height = getmeasuredheight(); } else { super.onmeasure(widthmeasurespec, heightmeasurespec); } } public void setexpanded(boolean expanded) { this.expanded = expanded; } }
expandableheightgridview ...
public class expandableheightgridview extends gridview { boolean expanded = false; public expandableheightgridview(context context) { super(context); } public expandableheightgridview(context context, attributeset attrs) { super(context, attrs); } public expandableheightgridview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } public boolean isexpanded() { return expanded; } @override public void onmeasure(int widthmeasurespec, int heightmeasurespec) { // hack! take android! if (isexpanded()) { // calculate entire height providing large height hint. // view.measured_size_mask represents largest height possible. int expandspec = measurespec.makemeasurespec(measured_size_mask, measurespec.at_most); super.onmeasure(widthmeasurespec, expandspec); viewgroup.layoutparams params = getlayoutparams(); params.height = getmeasuredheight(); } else { super.onmeasure(widthmeasurespec, heightmeasurespec); } } public void setexpanded(boolean expanded) { this.expanded = expanded; } }
mainactivity ...
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview (r.layout.activity_editor); intent intent = getintent(); m_file = new file (intent.getstringextra ("file_path")); m_filelength = (int) m_file.length(); // account non-complete line @ end of file ... if ((m_filelength % 8) == 0) m_numlines = (m_filelength / 8) + additional_lines; else m_numlines = (m_filelength / 8) + additional_lines + 1; // set index listview m_indexlistview = (expandableheightlistview) findviewbyid (r.id.index_listview); m_indexadapter = new indexadapter (getapplicationcontext(), m_numlines); m_indexlistview.setadapter (m_indexadapter); m_indexlistview.setexpanded(true); // file reader object hex/ascii adapters ... filereader filereader = new filereader (getapplicationcontext(), m_file); // set hex gridview m_hexgridview = (expandableheightgridview) findviewbyid (r.id.hex_gridview); m_hexadapter = new hexadapter (getapplicationcontext(), filereader); m_hexgridview.setadapter(m_hexadapter); m_hexgridview.setexpanded (true); // set ascii listview m_asciilistview = (expandableheightlistview) findviewbyid (r.id.ascii_listview); m_asciiadapter = new asciiadapter (getapplicationcontext(), m_numlines, filereader); m_asciilistview.setadapter(m_asciiadapter); m_asciilistview.setexpanded (true); m_hexgridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { view.setselected (true); } });
the 3 adapters read data given file , display accordingly.
does know how fix , make adapter views load necessary part?
edit: adapter code ...
hexadapter ...
public class hexadapter extends baseadapter { // constants // number of additional blank lines append ... private final int additional_lines = 30; // variables // context of editoractivity ... private context m_context; // reference file being edited ... private filereader m_filereader; // number of elements within file ... private int m_filelength; // number of total elements produce ... private int m_numelements; // functions public hexadapter (context c, filereader filereader) { m_context = c; m_filereader = filereader; m_filelength = (int) m_filereader.getlength(); int finishline = 8 - (m_filelength % 8); m_numelements = m_filelength + finishline + (additional_lines * 8); } public int getcount() { return m_numelements; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } // create new textview each item referenced adapter ... public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) m_context.getsystemservice (context.layout_inflater_service); view view; if (null == convertview) view = inflater.inflate (r.layout.editor_hex_element, parent, false); else view = convertview; // remove padding if rightmost element ... linearlayout container = (linearlayout) view.findviewbyid (r.id.gridview_hex_container); if ((position % 8) == 7) container.setpadding (0,0,0,0); else container.setpadding (0,0,1,0); string hextext; // if position within current file space, read file ... if (position < m_filelength) { // read file , generate hexadecimal text byte[] buffer = m_filereader.readbytesfromfile(position, 1); hextext = integer.tohexstring ((char)buffer[0]).touppercase(); // prepend 0 if length 1 ... if (hextext.length() == 1) hextext = "0" + hextext; } else { // set default un-edited value (00) ... hextext = "00"; } // textview instance , set text ... textview textview = (textview) view.findviewbyid (r.id.gridview_hex_edit); textview.settext (hextext); // set colour depending on file length ... if (position < m_filelength) textview.settextcolor (m_context.getresources ().getcolor (r.color.darkeraccentorange)); else textview.settextcolor (m_context.getresources ().getcolor (r.color.hex_gray)); return view; } // todo: add function update file length , numelements variables // public void updatefilelength (int numlength) ... }
asciiadapter ...
public asciiadapter (context c, int numlines, filereader filereader) { m_context = c; m_filereader = filereader; m_numlines = numlines; m_filelength = (int) filereader.getlength(); } public int getcount() { return m_numlines; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } // create new textview each item referenced adapter public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) m_context.getsystemservice (context.layout_inflater_service); view view; if (null == convertview) view = inflater.inflate (r.layout.editor_ascii_element, parent, false); else view = convertview; final int readposition = position * byte_size; final int fileremaining = m_filelength - readposition; final int readlength = (fileremaining >= byte_size) ? byte_size : fileremaining; byte[] buffer; stringbuilder builder = new stringbuilder (); // if of file remaining, read ... if (readlength > 0) { buffer = m_filereader.readbytesfromfile (readposition, readlength); // build read string ... (byte b : buffer) builder.append (convertbyte (b)); } // append remaining part of string (not read file) ... final int remaining = (readlength > 0) ? (byte_size - readlength) : byte_size; (int k = 0; k < remaining; k++) builder.append ('.'); textview textview = (textview) view.findviewbyid (r.id.ascii_textview); textview.settext (builder.tostring()); return view; } }
Comments
Post a Comment