I have this Autocomplete class and I am modifying the adapter so that, instead of just text, it will show an icon in front of the text that appears. All the choices available are stored in an array of strings.
Now, for some reason, it only shows 5 items no matter what I type.
public class AutoCompleteText extends Activity{
public class CustomAdapter extends ArrayAdapter<String>{
public CustomAdapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.arow,parent, false);
TextView label = (TextView) row.findViewById(R.id.element);
label.setText(listItems[position]);
ImageView icon = (ImageView) row.findViewById(R.id.image);
if("Item1".equals(listItems[position])|| something else || something else){
icon.setImageResource(R.drawable.img1);
}
else if("Item2".equals(listItems[position])|| something else || something else){
icon.setImageResource(R.drawable.img2);
}
else if("Item3".equals(listItems[position])|| something else || something else){
icon.setImageResource(R.drawable.img3);
}
else if("Item4".equals(listItems[position])|| something else || something else){
icon.setImageResource(R.drawable.img4);
}
else if("Item5".equals(listItems[position])|| something else || something else){
icon.setImageResource(R.drawable.img5);
}
else if("Item6".equals(listItems[position])|| something else || something else) {
icon.setImageResource(R.drawable.img6);
}
else{
icon.setImageResource(R.drawable.whatever);
}
return row;
}
}
AutoCompleteTextView acTextView;
private static String listItems[] = { "Item1", "Item2","Item3","Item4","Item5", "Item6", "Item7", "Item8", "Item9", "Item10"
"Item11", "Item12","Item13","Item14","Item15", "Item16", "Item17", "Item18", "Item19", "Item20");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
acTextView = (AutoCompleteTextView)findViewById(R.id.searchfield);
acTextView.setThreshold(1);
acTextView.setAdapter(new CustomAdapter(AutoCompleteText.this,R.layout.arow, listItems));
acTextView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
try {
//random staff
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
);
}
}
the app compiles and runs fine without any errors. Can anyone help me on this?
Thank you.
don’t compare
Strings with==since that checks if it is the same Object not the same text. Use.equalsinstead.It might work in your case since the compiler might be smart enough to use the same object to represent the text but it’ usually not a good idea.
Also you just have 5 (+default) items in your code. Do you see those?