带过滤的自动完成文本视图

时间:2015-08-04 13:25:38

标签: android android-fragments android-activity android-adapter android-filter

我正在开发一个应用程序,当我在TextField中输入文本时,它会提供保存在联系人列表中的匹配结果。但是当我在TextField中输入任何内容时,它会提供电话簿中保存的所有号码,当我点击搜索号码时,它也不会输入TextField

我的java代码

public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)
{
  View myView= inflater.inflate(R.layout.fragment1, container,false);
  LocalAdapter adapter = new LocalAdapter(this, MyActivity.phoneBook);
  AutoCompleteTextView textView = (AutoCompleteTextView) myView.findViewById(R.id.editText);
  textView.setAdapter(adapter);
  return myView;}
}

我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/myPic">
<AutoCompleteTextView
           android:id="@+id/editText"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:inputType="number"
           android:ems="16"
           android:singleLine="true"
        />
<ImageButton
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:clickable="true"
        android:baselineAlignBottom="false" android:adjustViewBounds="false"
        android:scaleType="fitCenter"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/editText" android:layout_alignParentStart="true"
        android:src="@android:drawable/stat_sys_phone_call"
/>

我的Adpater代码

public class LocalAdapter extends BaseAdapter implements Filterable
   {
    private ValueFilter valueFilter;
    private ArrayList<ContactInfo> mStringFilterList;
    private ArrayList<ContactInfo> _Contacts;
    Context context;
    ArrayList<ContactInfo> listData;

    LocalAdapter(Fragment myfrag, ArrayList<ContactInfo> listData{this.context = myfrag.getActivity();
    this.listData = listData;
    this.myfrag= myfrag;
    this.mStringFilterList=MyActivity.phoneBook;
    this._Contacts=MyActivity.phoneBook;
}
@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    View myRow = inflater.inflate(R.layout.tab4list, parent, false);
    ContactInfo rowData = listData.get(position);

    ImageView image = (ImageView) myRow.findViewById(R.id.imageView);
    image.setImageResource(rowData.image);

    TextView name = (TextView) myRow.findViewById(R.id.textView2);
    name.setText(rowData.contactName);

    TextView number = (TextView) myRow.findViewById(R.id.textView3);
    number.setText(rowData.contactNumber);

    myRow.setOnClickListener(new PassableOnClickListener(myfrag,position));

    return myRow;
}

public void add(ContactInfo obj)
{
    listData.add(obj);
    notifyDataSetChanged();
}

@Override
public Filter getFilter() {
    if(valueFilter==null) {

        valueFilter=new ValueFilter();
    }

    return valueFilter;
}
private class ValueFilter extends Filter {

    //Invoked in a worker thread to filter the data according to the constraint.
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results=new FilterResults();
        if(constraint!=null && constraint.length()>0){
            ArrayList<ContactInfo> filterList=new ArrayList<ContactInfo>();
            for(int i=0;i<mStringFilterList.size();i++){
                if((mStringFilterList.get(i).contactName.toUpperCase()).contains(constraint.toString().toUpperCase())) {
                    ContactInfo contacts = new ContactInfo(mStringFilterList.get(i).image,mStringFilterList.get(i).contactName,mStringFilterList.get(i).contactNumber);
                    contacts.contactName=mStringFilterList.get(i).contactName;
                    contacts.contactNumber=mStringFilterList.get(i).contactNumber;
                    filterList.add(contacts);
                }
            }
            results.count=filterList.size();
            results.values=filterList;
        }else{
            results.count=mStringFilterList.size();
            results.values=mStringFilterList;
        }
        return results;
    }


    //Invoked in the UI thread to publish the filtering results in the user interface.
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results)
    {
        _Contacts=(ArrayList<ContactInfo>) results.values;
        notifyDataSetChanged();
    }
}

}

请帮我解决这个问题。 先感谢您。 ![This is picture in which I was unable to select number from drop down list and also the number enter is not in the contacts but it is showing all the contacts ] 1

0 个答案:

没有答案