I am using an Autocomplete Textview which shows some names from database.I want to show a name in a textview which I selected from autocomplete textview.Here is my code:
我正在使用一个自动完成的Textview,它显示来自数据库的一些名称。我想在textview中显示我从autocomplete textview中选择的名称。这是我的代码:
ArrayList<String> s1 = new ArrayList<String>();
for (StudentInfo cn : studentInfo) {
s1.add(cn.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,s1);
a1.setThreshold(1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
7 个解决方案
#1
9
Try it this way:
试试这样:
AutoCompleteTextView a1 = (AutoCompleteTextView) findViewById(...);
StudentInfo[] s1 = studentInfo.toArray(new StudentInfo[studentInfo.size()]);
ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<StudentInfo>(this, android.R.layout.simple_dropdown_item_1line, s1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
Object item = parent.getItemAtPosition(position);
if (item instanceof StudentInfo){
StudentInfo student=(StudentInfo) item;
doSomethingWith(student);
}
}
});
The ArrayAdapter uses the toString() method of StudentInfo to generate the displayed texts, so you need to implement a nice toString method.
ArrayAdapter使用StudentInfo的toString()方法生成显示的文本,因此需要实现一个很好的toString方法。
This way, this kind of implementation can be adapted to any object type.
通过这种方式,这种实现可以适用于任何对象类型。
Btw.: i prefer android.R.layout.simple_spinner_dropdown_item instead of android.R.layout.simple_dropdown_item_1line
顺便说一句。:我喜欢android.R.layout。simple_spinner_dropdown_item代替android.R.layout.simple_dropdown_item_1line
#2
3
your s1
contains all the names from database
您的s1包含来自数据库的所有名称
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d("your selected item",""+s1.get(position));
//s1.get(position) is name selected from autocompletetextview
// now you can show the value on textview.
}
});
hope this will helps you,
希望这能对你有所帮助,
#3
3
From the view object arg1 get the value of the String. From the ArrayList supplied to the AutoCompleteTextView get the position of the item using this String.
从视图对象arg1获取字符串的值。从提供给AutoCompleteTextView的ArrayList中,使用此字符串获取项的位置。
in your case the code would look something like below.
在您的例子中,代码应该如下所示。
int selectedPos = s1.indexOf((String) ((TextView) arg1).getText());
int selectedPos = s1.indexOf(String) (TextView) arg1).getText();
selectedPos is the position of the string in the supplied ArrayList.
selectedPos是提供的ArrayList中字符串的位置。
#4
3
Gratitude to Stefan Richter! I would like to add that it is possible to use List<T>
directly when you construct the adapter:
感谢斯蒂芬·里克特!我想补充的是,在构建适配器时,可以直接使用List
AutoCompleteTextView autoCompleteTextView = dialogView.findViewById(R.id.autoComplete);
// Where mStudentsInfo is List<StudentInfo>
ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, mStudentsInfo);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
if (item instanceof StudentInfo) {
StudentInfo studentInfo = (StudentInfo) item;
// do something with the studentInfo object
}
}
});
And also do not forget to override the toString()
method in StudentInfo.class
:
也不要忘记在StudentInfo.class中覆盖toString()方法。
public class StudentInfo {
....
@Override
public String toString() {
return studentName;
}
}
#5
2
overriding toString
method for model class (StudenInfo in this case) is not a good idea!
If you only want to get the text of selected item, use this code:
重写模型类的toString方法(在本例中为StudenInfo)不是一个好主意!如果您只想获取所选项目的文本,请使用以下代码:
autoCompleteView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
// here is your selected item
}
});
#6
1
To get selected item from autocomplete selection which uses user defined datatype and sets values of related list.following code worked for me
要从使用用户定义的数据类型和设置相关列表值的自动完成选择中获取所选项。以下代码为我工作。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
int selectedPos = getYourList().indexOf((((TextView)view).getText()).toString());
SomeDAO dao = getSomeDaoList().get(selectedPos);
//do your code
}
Note: I have changed default parameter names in onItemClick as arg0-parent, arg1-view,arg2-position & SomeDAO is user-defined datatype
注意:我已经将onItemClick中的默认参数名称更改为arg0-parent、arg1-view、arg2-position & SomeDAO是用户定义的数据类型
#7
0
Use parent position is '0'
使用父位置为'0'
txt_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {
Object item = parent.getItemAtPosition(0);
if (item instanceof MYData{
MYData data=(MYData) item;
String dd = data.getName();
}
}
});
#1
9
Try it this way:
试试这样:
AutoCompleteTextView a1 = (AutoCompleteTextView) findViewById(...);
StudentInfo[] s1 = studentInfo.toArray(new StudentInfo[studentInfo.size()]);
ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<StudentInfo>(this, android.R.layout.simple_dropdown_item_1line, s1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
Object item = parent.getItemAtPosition(position);
if (item instanceof StudentInfo){
StudentInfo student=(StudentInfo) item;
doSomethingWith(student);
}
}
});
The ArrayAdapter uses the toString() method of StudentInfo to generate the displayed texts, so you need to implement a nice toString method.
ArrayAdapter使用StudentInfo的toString()方法生成显示的文本,因此需要实现一个很好的toString方法。
This way, this kind of implementation can be adapted to any object type.
通过这种方式,这种实现可以适用于任何对象类型。
Btw.: i prefer android.R.layout.simple_spinner_dropdown_item instead of android.R.layout.simple_dropdown_item_1line
顺便说一句。:我喜欢android.R.layout。simple_spinner_dropdown_item代替android.R.layout.simple_dropdown_item_1line
#2
3
your s1
contains all the names from database
您的s1包含来自数据库的所有名称
a1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d("your selected item",""+s1.get(position));
//s1.get(position) is name selected from autocompletetextview
// now you can show the value on textview.
}
});
hope this will helps you,
希望这能对你有所帮助,
#3
3
From the view object arg1 get the value of the String. From the ArrayList supplied to the AutoCompleteTextView get the position of the item using this String.
从视图对象arg1获取字符串的值。从提供给AutoCompleteTextView的ArrayList中,使用此字符串获取项的位置。
in your case the code would look something like below.
在您的例子中,代码应该如下所示。
int selectedPos = s1.indexOf((String) ((TextView) arg1).getText());
int selectedPos = s1.indexOf(String) (TextView) arg1).getText();
selectedPos is the position of the string in the supplied ArrayList.
selectedPos是提供的ArrayList中字符串的位置。
#4
3
Gratitude to Stefan Richter! I would like to add that it is possible to use List<T>
directly when you construct the adapter:
感谢斯蒂芬·里克特!我想补充的是,在构建适配器时,可以直接使用List
AutoCompleteTextView autoCompleteTextView = dialogView.findViewById(R.id.autoComplete);
// Where mStudentsInfo is List<StudentInfo>
ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, mStudentsInfo);
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
if (item instanceof StudentInfo) {
StudentInfo studentInfo = (StudentInfo) item;
// do something with the studentInfo object
}
}
});
And also do not forget to override the toString()
method in StudentInfo.class
:
也不要忘记在StudentInfo.class中覆盖toString()方法。
public class StudentInfo {
....
@Override
public String toString() {
return studentName;
}
}
#5
2
overriding toString
method for model class (StudenInfo in this case) is not a good idea!
If you only want to get the text of selected item, use this code:
重写模型类的toString方法(在本例中为StudenInfo)不是一个好主意!如果您只想获取所选项目的文本,请使用以下代码:
autoCompleteView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
// here is your selected item
}
});
#6
1
To get selected item from autocomplete selection which uses user defined datatype and sets values of related list.following code worked for me
要从使用用户定义的数据类型和设置相关列表值的自动完成选择中获取所选项。以下代码为我工作。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
int selectedPos = getYourList().indexOf((((TextView)view).getText()).toString());
SomeDAO dao = getSomeDaoList().get(selectedPos);
//do your code
}
Note: I have changed default parameter names in onItemClick as arg0-parent, arg1-view,arg2-position & SomeDAO is user-defined datatype
注意:我已经将onItemClick中的默认参数名称更改为arg0-parent、arg1-view、arg2-position & SomeDAO是用户定义的数据类型
#7
0
Use parent position is '0'
使用父位置为'0'
txt_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {
Object item = parent.getItemAtPosition(0);
if (item instanceof MYData{
MYData data=(MYData) item;
String dd = data.getName();
}
}
});