Getview Of Custom Arrayadapter For Autocompletetextview Not Called In Android?
I want to use AutoCompleteTextView using a custom ArrayAdapter. I decided to use an Arrayadapter. But in my custom ArrayAdapter getView() is not called, AutoCompleteTextView is n
Solution 1:
The following code:
publicclassMainActivityextendsAppCompatActivity {
AutoCompleteTextView text;
String[] languages = {"Android ", "java", "IOS", "SQL", "JDBC", "Web services"};
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Names> names = initNameArray();
text = (AutoCompleteTextView) findViewById(R.id.autocompleteTvId);
text.setThreshold(1);
CustomArrayAdapteradapter=newCustomArrayAdapter(this, R.layout.recent_text, names);
text.setAdapter(adapter);
}
private List<Names> initNameArray() {
List<Names> recent_search = newArrayList<>();
inti=0;
for (String s : languages) {
Namesnames=newNames();
names.setName(s);
recent_search.add(i++, names);
}
return recent_search;
}
//custom CustomArrayAdapterpublicclassCustomArrayAdapterextendsArrayAdapter<Names> {
List<Names> names;
Context context;
int layoutResourceId;
publicCustomArrayAdapter(Context context, int resource, List<Names> objects) {
super(context, resource, objects);
names = objects;
this.context = context;
layoutResourceId = resource;
}
@OverridepublicintgetCount() {
returnsuper.getCount();
}
@Overridepublic Names getItem(int position) {
return names.get(position);
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
try {
/*
* The convertView argument is essentially a "ScrapView" as described is Lucas post
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/if (convertView == null) {
// inflate the layoutLayoutInflaterinflater= (LayoutInflater) ((context)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, parent, false);
}
// object item based on the positionNamesobjectItem= getItem(position);
// get the TextView and then set the text (item name) and tag (item ID) valuesTextViewtextViewItem= (TextView) convertView.findViewById(R.id.tv_recent_text);
textViewItem.setText(objectItem.getName());
// in case you want to add some style, you can do something like:
textViewItem.setBackgroundColor(Color.CYAN);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
//my pojo classpublicclassNames {
String name;
public String getName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
}
}
should work just fine for you. The only difference from your code is the List instead of array. In order to make it work, delete hello world and start type 'An', 'Ja', etc.
Post a Comment for "Getview Of Custom Arrayadapter For Autocompletetextview Not Called In Android?"