Android - Using SimpleCursorAdapter in AutoCompleteTextView gives CursorIndexOutofBoundsException when user selects 3rd suggestion onwards -
sorry mouthful of title, have cut down because exceeded 150 character limit.
i have autocompletetextview
(actv) , using simplecursoradapter
since normal actv searches user input @ start of each substring (substring separated whitespaces) , not within substrings. example, having list adipose
, bad wolf
, searching ad
show adipose
, not bad wolf
. made adapter shown below:
//create actv here autocompletetextview search = (autocompletetextview) findviewbyid(r.id.actvcataloguesearch); search.setthreshold(1); string[] = { "name" }; int[] = { android.r.id.text1 }; simplecursoradapter cursoradapter = new simplecursoradapter(this, android.r.layout.simple_dropdown_item_1line, null, from, to, 0); cursoradapter.setstringconversioncolumn(1); filterqueryprovider provider = new filterqueryprovider(){ @override public cursor runquery(charsequence constraint) { // todo auto-generated method stub string constrain = (string) constraint; constrain = constrain.touppercase(); log.d("hi", "runquery constraint: " + constraint); if (constraint == null) { return null; } string[] columnnames = { columns._id, "name" }; matrixcursor c = new matrixcursor(columnnames); try { (int = 0; < pdflist.length; i++) { if(pdflist[i].contains(constrain)){ log.d("hello","match! pdflist item = " + pdflist[i]); c.newrow().add(i).add(pdflist[i]); } } } catch (exception e) { e.printstacktrace(); } return c; } }; cursoradapter.setfilterqueryprovider(provider); search.setadapter(cursoradapter);
this code enables me show other list items contains substring user input.
now, trying make onitemclicklistener
function properly. here have far:
search.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { matrixcursor matrix = (matrixcursor)parent.getitematposition(position); log.d("hello", "matrix values = " + matrix); string selection = matrix.getstring(position); log.d("hallo","selection = " + selection); log.d("hello","item id @ position = " + parent.getitemidatposition(position)); int pos = (int) parent.getitemidatposition(position); log.d("sup", "position = " + pos); string path = imagelist[pos].getabsolutepath(); openpdfintent(path); } });
here, trying matrixcursor
element @ given position. works fine of user selects first 2 suggestions. however, when user clicks 3rd suggestion onwards, application throws cursorindexoutofboundsexception requested column: 2, # of columns: 2
clicking on logcat lines pointed me code string selection = matrix.getstring(position);
i think doing matrix.getstring(position)
causes error since getstring returns value of requested column string, , since there 2 columns, selecting suggestion in actv position (position shown user, not position of said item in list) greater 2 causes code screw up.
my question is, there better way string value of selected item given using simplecursoradapter
? i've looked on documentation of matrix cursor in android dev site , can't find way row/element based on position.
any appreciated.
edit:
using matrix.movetofirst();
such did not well:
search.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { matrixcursor matrix = (matrixcursor)parent.getitematposition(position); if(matrix != null) { if(matrix.getcount() > 0) { matrix.movetofirst(); string selection = matrix.getstring(position); int pos = (int) parent.getitemidatposition(position); string path = imagelist[pos].getabsolutepath(); openpdfintent(path); } } } });
and still got exception:
android.database.cursorindexoutofboundsexception: requested column: 4, # of columns: 2
the requested column 4
position of selected actv suggestion, indexed zero.
try out this
matrixcursor matrix = ............. log.d("hello", "matrix values = " + matrix); /***** check here cursor not null *****/ if(matrix != null) { if(matrix.getcount() > 0) { matrix.movetofirst(); /*** stuff here.... **/ } }
Comments
Post a Comment