自定义autocompletetextview空间不起作用

时间:2014-02-10 06:16:38

标签: android autocompletetextview

我有一个AutoCompleteTextView,它有两个值name和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);
 }
});

1 个答案:

答案 0 :(得分: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);

对于适配器Layout 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>

希望这有帮助。