问题描述
我使用的是自定义适配器与AutoCompleteTextView。它工作正常,在模拟器上,我的平板电脑。然而,在横向模式下的一个问题在我的手机。被显示在此模式下,自动完成的提示是对象的信息,而不是文字。但是,当我选择任何项目的字段被正确地在各自领域的文本填充。
自动完成其他字段是基于Android股票阵列适配器工作正常。
我必须做些事情在我的自定义适配器?我看到SO只有一个类似的问题。对于这个问题的一个回应是在谈论覆盖toString方法,但我不明白它足够的在我的code来实现。
任何指导,将大大AP preciated?请让我知道如果你需要更多的信息。
编辑:添加我的自定义适配器源$ C $ C ....
公共类Part_Mstr_Info
{
专用长PART_ID;
私人字符串名称,desg,组织,部门;
公共Part_Mstr_Info(长PART_ID,字符串名称,字符串desg,组织字符串,字符串部)
{
this.part_id = PART_ID;
this.name =名称;
this.desg = desg;
this.org =组织;
this.dept =部门;
}
众长get_part_id(){返回PART_ID; }
公共字符串get_name(){返回名称; }
公共字符串get_desg(){返回desg; }
公共字符串get_org(){返回组织; }
公共字符串get_dept(){返回部门; }
}
公共类CustomAdapter扩展ArrayAdapter< Part_Mstr_Info>实现过滤的{
私人列表< Part_Mstr_Info>项;
私人的ArrayList< Part_Mstr_Info>原稿;
私人活动的活动;
私人ArrayFilter myFilter;
公共CustomAdapter(活动一,INT textViewResourceId,ArrayList的< Part_Mstr_Info>项){
超(一,textViewResourceId,项);
this.entries =条目;
this.activity =一个;
}
公共静态类ViewHolder {
公众的TextView tv_ac_name;
公众的TextView tv_ac_desg;
公众的TextView tv_ac_org;
公众的TextView tv_ac_dept;
}
@覆盖
公众诠释getCount将(){
返回条目!= NULL? entries.size():0;
}
@覆盖
公共Part_Mstr_Info的getItem(INT指数){
返回entries.get(指数);
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
视图V = convertView;
ViewHolder持有人;
如果(V == NULL){
LayoutInflater VI =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
V = vi.inflate(R.layout.ac_name_list,NULL);
持有人=新ViewHolder();
holder.tv_ac_name =(TextView中)v.findViewById(R.id.ac_name);
holder.tv_ac_desg =(TextView中)v.findViewById(R.id.ac_desg);
holder.tv_ac_org =(TextView中)v.findViewById(R.id.ac_org);
holder.tv_ac_dept =(TextView中)v.findViewById(R.id.ac_dept);
v.setTag(保持器);
}
其他
支架=(ViewHolder)v.getTag();
最后Part_Mstr_Info定制= entries.get(位置);
如果(自定义!= NULL){
holder.tv_ac_name.setText(custom.get_name());
holder.tv_ac_desg.setText(custom.get_desg());
holder.tv_ac_org.setText(custom.get_org());
holder.tv_ac_dept.setText(custom.get_dept());
}
返回伏;
}
@覆盖
公共过滤用getFilter(){
如果(myFilter == NULL){
myFilter =新ArrayFilter();
}
返回myFilter;
}
@覆盖
公共字符串的toString(){
字符串TEMP =的getClass()的getName()。
返回温度;
}
私有类ArrayFilter扩展过滤器{
@覆盖
保护FilterResults performFiltering(CharSequence的约束){
FilterResults结果=新FilterResults();
如果(原稿== NULL)
原稿=新的ArrayList< Part_Mstr_Info>(项);
如果(约束=空&安培;!&安培;!constraint.length()= 0){
ArrayList的< Part_Mstr_Info> resultsSuggestions =新的ArrayList< Part_Mstr_Info>();
的for(int i = 0; I<原稿尺寸();我++){
如果(orig.get(ⅰ).get_name()。与toLowerCase()。startsWith(constraint.toString()。与toLowerCase())){
resultsSuggestions.add(orig.get(ⅰ));
}
}
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
}
其他 {
ArrayList的< Part_Mstr_Info>名单=新的ArrayList< Part_Mstr_Info>(原稿);
results.values =清单;
results.count =则为list.size();
}
返回结果;
}
@覆盖
@燮pressWarnings(未登记)
保护无效publishResults(CharSequence的约束,FilterResults结果){
明确();
ArrayList的< Part_Mstr_Info> newValues =(ArrayList的< Part_Mstr_Info>)results.values;
如果(newValues!= NULL){
的for(int i = 0; I< newValues.size();我++){
添加(newValues.get(一));
}
如果(results.count大于0){
notifyDataSetChanged();
} 其他 {
notifyDataSetInvalidated();
}
}
}
}
创建它扩展自定义的类< AutoCompleteTextView />
。例如:
包lt.irisas.akcija.utils;
进口lt.irisas.akcija.data.ItemsModel;
进口android.content.Context;
进口android.database.Cursor;
进口android.util.AttributeSet;
进口android.widget.AutoCompleteTextView;
/ **定制AutoCompleteTextView返回光标列信息为String
* /
公共类CustomAutoCompleteTextView扩展AutoCompleteTextView {
公共CustomAutoCompleteTextView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
/ *覆盖此方法,并返回字符串类型解决了问题* /
@覆盖
受保护的CharSequence convertSelectionToString(对象将selectedItem){
光标C =(光标)将selectedItem;
返回c.getString(c.getColumnIndex(COLUMN_NAME));
}
}
但
而不是< AutoCompleteTextView />
您必须使用自定义的。例如:
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
< lt.irisas.akcija.utils.CustomAutoCompleteTextView
机器人:ID =@ + ID / search_field
机器人:layout_width =0dip
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:单线=真
机器人:imeOptions =actionSearch>
< /lt.irisas.akcija.utils.CustomAutoCompleteTextView>
< / LinearLayout中>
希望它帮助。嗯,至少它为我做的。
I am using a Custom Adapter with AutoCompleteTextView. It works fine on the emulator and my tablet. However, there is an issue on my phone in landscape mode. The auto complete hints being shown in this mode are object info rather than text. However, when I select any item the fields gets populated correctly with text in the respective fields.
Auto Complete for other fields that are based on Android Stock Array Adapter works fine.
Do I have to do something for this in my Custom Adapter? I saw only one similar question on SO. One response for that question was talking about overriding the toString method but I couldn't understand it enough to implement in my code.
Any guidance will be greatly appreciated? Please let me know if you need more information.
EDIT: Added my custom adapter source code....
public class Part_Mstr_Info
{
private long part_id;
private String name, desg, org, dept;
public Part_Mstr_Info(long part_id, String name, String desg, String org, String dept)
{
this.part_id = part_id;
this.name = name;
this.desg = desg;
this.org = org;
this.dept = dept;
}
public long get_part_id() { return part_id; }
public String get_name() { return name; }
public String get_desg() { return desg; }
public String get_org() { return org; }
public String get_dept() { return dept; }
}
public class CustomAdapter extends ArrayAdapter<Part_Mstr_Info> implements Filterable{
private List<Part_Mstr_Info> entries;
private ArrayList<Part_Mstr_Info> orig;
private Activity activity;
private ArrayFilter myFilter;
public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Part_Mstr_Info> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView tv_ac_name;
public TextView tv_ac_desg;
public TextView tv_ac_org;
public TextView tv_ac_dept;
}
@Override
public int getCount(){
return entries!=null ? entries.size() : 0;
}
@Override
public Part_Mstr_Info getItem(int index) {
return entries.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.ac_name_list, null);
holder = new ViewHolder();
holder.tv_ac_name = (TextView) v.findViewById(R.id.ac_name);
holder.tv_ac_desg = (TextView) v.findViewById(R.id.ac_desg);
holder.tv_ac_org = (TextView) v.findViewById(R.id.ac_org);
holder.tv_ac_dept = (TextView) v.findViewById(R.id.ac_dept);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Part_Mstr_Info custom = entries.get(position);
if (custom != null) {
holder.tv_ac_name.setText(custom.get_name());
holder.tv_ac_desg.setText(custom.get_desg());
holder.tv_ac_org.setText(custom.get_org());
holder.tv_ac_dept.setText(custom.get_dept());
}
return v;
}
@Override
public Filter getFilter() {
if (myFilter == null){
myFilter = new ArrayFilter();
}
return myFilter;
}
@Override
public String toString() {
String temp = getClass().getName();
return temp;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (orig == null)
orig = new ArrayList<Part_Mstr_Info>(entries);
if (constraint != null && constraint.length() != 0) {
ArrayList<Part_Mstr_Info> resultsSuggestions = new ArrayList<Part_Mstr_Info>();
for (int i = 0; i < orig.size(); i++) {
if(orig.get(i).get_name().toLowerCase().startsWith(constraint.toString().toLowerCase())){
resultsSuggestions.add(orig.get(i));
}
}
results.values = resultsSuggestions;
results.count = resultsSuggestions.size();
}
else {
ArrayList <Part_Mstr_Info> list = new ArrayList <Part_Mstr_Info>(orig);
results.values = list;
results.count = list.size();
}
return results;
}
@Override
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
ArrayList<Part_Mstr_Info> newValues = (ArrayList<Part_Mstr_Info>) results.values;
if(newValues !=null) {
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if(results.count>0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
Create custom class which extends <AutoCompleteTextView />
. For example:
package lt.irisas.akcija.utils;
import lt.irisas.akcija.data.ItemsModel;
import android.content.Context;
import android.database.Cursor;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;
/** Customizing AutoCompleteTextView to return Cursor column info as String
*/
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/* Overriding this method and returning String type solves the problem */
@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
Cursor c = (Cursor) selectedItem;
return c.getString(c.getColumnIndex("COLUMN_NAME"));
}
}
But instead of <AutoCompleteTextView/>
you must use your custom one. For example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<lt.irisas.akcija.utils.CustomAutoCompleteTextView
android:id="@+id/search_field"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:imeOptions="actionSearch" >
</lt.irisas.akcija.utils.CustomAutoCompleteTextView>
</LinearLayout>
Hope it helps. Well at least it did for me.
这篇关于Android的AutoCompleteTextView显示对象的信息,而不是文本在横向模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!