自定义的AutocompleteTextView空格无法使用

3

我有一个AutoCompleteTextView,其中只有名称和id两个值,当用户输入时,只显示名称。我遇到的问题是当用户在键入某些字母后按下空格键,相关的值会向下移动。我找不到问题所在,能否有人帮忙。

AutoCompleteTextView CustomerNameAutoFill = (AutoCompleteTextView)findViewById(R.id.AutoCompleteCustName);

ArrayList<Map<String, String>> mPeopleList = new ArrayList<Map<String, String>>();
SimpleAdapter mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview, new String[] { "Customer_name", "Customer_id" }, new int[] { R.id.customer_name, R.id.customer_id });

//Inserting all values in the autocompletetextview 
try 
{
    DatabaseHandler db = new DatabaseHandler(this);
    db.open();
    Cursor c = db.getAllRecordsFromCustomerMaster0();
    int i = 0;

    if (c.moveToFirst())
    {
        do 
         {  
            Map<String, String> NamePhoneType = new HashMap<String, String>();
            NamePhoneType.put("Customer_name", c.getString(1));
            NamePhoneType.put("Customer_id", c.getString(0));

            mPeopleList.add(NamePhoneType);
            i++;
        }while (c.moveToNext());
    }
    else {
        Toast.makeText(CustomerDailyAnalysis.this, "No Customer Name.Please Sync data.", Toast.LENGTH_LONG).show();
    }

    db.close();
 } 
catch (Throwable e)  {
     Log.d("SQL Error",e+"");
}

CustomerNameAutoFill.setThreshold(1);
CustomerNameAutoFill.setAdapter(mAdapter);


CustomerNameAutoFill.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, int position, long id) 
 {
    Map<String, String> map = (Map<String, String>)listView.getItemAtPosition(position);

    String Customer_name  = map.get("Customer_name");
    String Customer_id    = map.get("Customer_id");
    CustomerNameAutoFill.setText(Customer_name);

    saveInPreference("CustomerMasterNameForCDA", Customer_name);
    saveInPreference("CustomerMasterIdForCDA", Customer_id);

    Log.d("Log", "CustomerMasterNameForCDA:  "+ Customer_name + "  CustomerMasterIdForCDA" + Customer_id);
 }
});

你需要创建一个自定义适配器,并使用筛选器,访问此链接 - Imtiyaz Khalani
1个回答

0

Khalani 是对的。这是我的实现,使用了广泛使用的集合 Map 类。

package com.urugn.autocomplete.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import com.urugn.android.v_route_info.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author UruGN
 */
public class PlacesAdapter extends ArrayAdapter {

    private final String MY_DEBUG_TAG = "MapAdapter";
    private ArrayList<Map> items;
    private ArrayList<Map> itemsAll;
    private ArrayList<Map> suggestions;

    private int viewResourceId;
    private final String key = "description";


    public PlacesAdapter(Context context, int resource, int  textViewResourceId, List objects) {
        super(context, resource, textViewResourceId, objects);
        viewResourceId = textViewResourceId;
        itemsAll = new ArrayList(objects);
        suggestions = new ArrayList(objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        Map map = (Map) getItem(position);
        if (map != null) {
            if (v != null) {
                TextView vriPlace = (TextView) v.findViewById(R.id.vriPlace);
                if (vriPlace != null) {
                    vriPlace.setText((String) map.get(key));

                }
            }
        }
        return v;
    }

    /**
     *
     * @return
     */
    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    Filter nameFilter = new Filter() {
        @Override
        public String convertResultToString(Object resultValue) {
            String str = (String) ((Map) resultValue).get(key);
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            Log.d("VRIPlacesFilter", "Filter " + constraint);
            if (constraint != null) {
                suggestions.clear();
                for (Map map : itemsAll) {
                    if (((String) map.get(key)).toLowerCase().startsWith(constraint.toString().toLowerCase())) {

                        Log.d("VRIPlacesFilter", "Found " + map.get(key));
                        suggestions.add(map);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.d("VRIPlacesFilter", "Publish " + constraint + " " + results.count);
            if (results != null && results.count > 0) {
                ArrayList<Map> filteredList = (ArrayList<Map>) results.values;
                clear();
                for (Map c : filteredList) {
                    add(c);
                    Log.d("VRIPlacesFilter", "Publish " + c.get(key));
                }

//                notifyDataSetChanged();
            }

            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                //notifyDataSetInvalidated();
            }
        }
    };

}

为AutoCompleteTextView设置适配器

List<Map<String, String>> places = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>(); 
map.put("description", "My place");
map.put("description", "Her place");
map.put("description", "Our place");
map.put("description", "There place");
map.put("description", "The place");
places.add(map);

PlacesAdapter adapter = new PlacesAdapter(getBaseContext(), R.layout.auto_complete, R.id.vriPlace, places);

AutoCompleteTextView myAuto = new AutoCompleteTextView();
myAuto.setAdapter(adapter);

至于适配器布局auto_complete.xml,我有以下内容。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView
        android:id="@+id/vriPlace"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#ff7e00"
        android:textAppearance="?android:attr/textAppearanceLarge"
    />
</LinearLayout>

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接