Hello I have an activty which has a listview, autocomplete textview which also if user fills in his entry, it filters the listview. Now when I would click the item from my string array, it’s not getting to the intended activity. Please check my code below:
private AutoCompleteTextView tv;
ArrayList<String> classesList = new ArrayList<String>( Arrays.asList( classes ) );
int textlength = 0;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Integer> image_sort = new ArrayList<Integer>();
lv = ( ListView ) findViewById( R.id.lvFruits );
lv.setTextFilterEnabled(true);
lv.setAdapter( new MyCustomAdapter( classes, imagesID ) );
lv.setOnItemClickListener( this );
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.my_list_item, classesList );
tv = (AutoCompleteTextView) findViewById (R.id.txtSearchFruit);
tv.setThreshold( 1 );
tv.setAdapter( adapter );
tv.setTextColor( Color.BLACK );
/**
* Enabling Search Filter
* */
tv.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
textlength = tv.getText().length();
text_sort.clear();
image_sort.clear();
for(int i=0; i < classes.length; i++)
{
if (textlength <= classes[i].length())
{
if (tv.getText().toString().
equalsIgnoreCase((String) classes[i].subSequence(0, textlength)))
{
text_sort.add( classes[i] );
image_sort.add( imagesID[i] );
}
}
}
lv.setAdapter(new MyCustomAdapter
(text_sort, image_sort));
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
String selected = classes[lv.getFirstVisiblePosition()+position];
if( selected.equals("Apple") ){
Intent apple = new Intent(Fruits.this, Apples.class);
startActivity(apple);
}
else if( selected.equals("Apricot") ){
Intent apricot = new Intent(Fruits.this, Apricots.class);
startActivity(apricot);
}
else if( selected.equals("Avocado") ){
Intent avocado = new Intent(Fruits.this, Avocado.class);
startActivity(avocado);
}
}
final String[] classes = new String[] {
"Apple", // #0
"Apricot", // #1
"Avocado" // #2 }
What’s happening is that, when I chose Avocado, it goes to Apples Activivty. Any help is appreciated. Thanks.
When you filter your ListView you change the index, so you cannot rely on
positionto fetch the correct row fromclasses. Simply fetch the word in the row:It looks like you created a unique Activity for item in
classes… That is a lot of work, you should try to create one generic class that can load the appropriate data depending on the user’s selection. This approach is much faster to write and easier to maintain.