您好,我有一个活动,它有一个列表视图,自动完成文本视图,如果用户填写他的条目,它也会过滤列表视图。现在,当我从我的字符串数组中单击该项目时,它并没有达到预期的活动。请在下面检查我的代码:
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 }
发生的事情是,当我选择 Avocado 时,它去了 Apples Activivty。任何帮助表示赞赏。谢谢。