我想使用自定义AutoCompleteTextView
ArrayAdapter
。我决定使用Arrayadapter
。
但是在我的自定义ArrayAdapter
getView()
未调用时,适配器未设置AutoCompleteTextView
。
下面我可以看到我到目前为止所尝试的内容:
MainActivity.java
:
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView text;
String[] languages = { "Android ", "java", "IOS", "SQL", "JDBC", "Web services" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Names[] names = this.initNameArray();
this.text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
this.text.setThreshold(1);
adapter adapter = new adapter(this, R.layout.recent_text, names);
this.text.setAdapter(adapter);
}
private Names[] initNameArray() {
Names[] recent_search = new Names[this.languages.length];
int i = 0;
for (String s : this.languages) {
Names names = new Names();
names.setName(s);
recent_search[i++] = names;
}
return recent_search;
}
// custom adapter
public class adapter extends ArrayAdapter<Names> {
Names names[];
Context context;
int layoutResourceId;
public adapter(Context context, int resource, Names[] objects) {
super(context, resource, objects);
this.names = objects;
this.context = context;
this.layoutResourceId = resource;
}
@Override
public int getCount() {
return this.names.length;
}
@Override
public Names getItem(int position) {
return this.names[position];
}
@Override
public 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 layout
LayoutInflater inflater = (LayoutInflater) ((this.context))
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(this.layoutResourceId, parent, false);
}
// object item based on the position
Names objectItem = this.getItem(position);
// get the TextView and then set the text (item name) and tag
// (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(R.id.recent_search);
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 class
public class Names {
String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
答案 0 :(得分:0)
以下代码:
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView text;
String[] languages = {"Android ", "java", "IOS", "SQL", "JDBC", "Web services"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Names> names = initNameArray();
text = (AutoCompleteTextView) findViewById(R.id.autocompleteTvId);
text.setThreshold(1);
CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.recent_text, names);
text.setAdapter(adapter);
}
private List<Names> initNameArray() {
List<Names> recent_search = new ArrayList<>();
int i = 0;
for (String s : languages) {
Names names = new Names();
names.setName(s);
recent_search.add(i++, names);
}
return recent_search;
}
//custom CustomArrayAdapter
public class CustomArrayAdapter extends ArrayAdapter<Names> {
List<Names> names;
Context context;
int layoutResourceId;
public CustomArrayAdapter(Context context, int resource, List<Names> objects) {
super(context, resource, objects);
names = objects;
this.context = context;
layoutResourceId = resource;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public Names getItem(int position) {
return names.get(position);
}
@Override
public 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 layout
LayoutInflater inflater = (LayoutInflater) ((context)).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, parent, false);
}
// object item based on the position
Names objectItem = getItem(position);
// get the TextView and then set the text (item name) and tag (item ID) values
TextView textViewItem = (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 class
public class Names {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
应该对你有用。与代码的唯一区别是List而不是数组。为了使其工作,删除hello world并开始键入&#39; An&#39;,&#39; Ja&#39;等。