简体   繁体   中英

“No Selected Item ” While AutoCompleteTextview item Clicked

AutoComplete texts are listing correctly Everything works fine ..But the problem is Once i click the autocomplete item,it is not selecting.

Logcat shows "No Selected Item" when i clicked the items.

Autocomplte.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // Log.d("your selected item",""+s1.get(position));
            System.out.println("sajdksadkasjdksajdksa");
            // s1.get(position) is name selected from autocompletetextview
            // now you can show the value on textview.
        }
    });

//--------------Adapter class

public class LoadLocationBasedonCountry extends BaseAdapter implements
    Filterable {

private ArrayList<CountryLocation> locList;
private ArrayList<CountryLocation> storedLocList;
private Activity mActivity;
private LayoutInflater inflater;


public LoadLocationBasedonCountry(Activity activity,
        ArrayList<CountryLocation> joes) {
    this.mActivity = activity;
    this.locList = joes;
    storedLocList = new ArrayList<CountryLocation>(joes);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    View vi = convertView;
    if (vi == null) {
        holder = new ViewHolder();
        inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.row_loc_based_on_country, null,false);
        holder.txtCtyName = (TextView) vi.findViewById(R.id.txtCtyName);
        vi.setTag(holder);

    }

    else {
        holder = (ViewHolder) vi.getTag();

    }

    CountryLocation cLoc = locList.get(position);
    holder.txtCtyName.setText(cLoc.getLocName() + ", " + cLoc.getCountry());


    return vi;
}

@Override
public int getCount() {
    return locList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

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

public static class ViewHolder {
    private TextView txtCtyName;

}

@Override
public Filter getFilter() {
    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {
            locList = (ArrayList<CountryLocation>) results.values;

            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint)

        {

            FilterResults results = new FilterResults();
            ArrayList<CountryLocation> FilteredArrList = new ArrayList<CountryLocation>();

            if (constraint == null || constraint.length() == 0) {
                results.count = storedLocList.size();
                results.values = storedLocList;
            } else {
                constraint = constraint.toString();

                for (int i = 0; i < storedLocList.size(); i++) {
                    CountryLocation country = storedLocList.get(i);
                    if (country.getLocName()
                            .toLowerCase(Locale.getDefault())
                            .startsWith(constraint.toString())) {
                        FilteredArrList.add(country);
                    }
                }

                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }

            return results;
        }
    };

    return filter;
}

public void updateList(ArrayList<CountryLocation> list) {
    locList.clear();
    locList = list;
    notifyDataSetChanged();
}

}

Is anybody came across these type of issues?

Finally i got the solution;

Since in autocompleteTextview,it gets value using adapter.getItem(position) using the method "performCompletion" ----- refer android.widget.AutoCompleteTextview

sample:

 private void performCompletion(View selectedView, int position, long id) {
    if (isPopupShowing()) {
        Object selectedItem;
        if (position < 0) {
            selectedItem = mPopup.getSelectedItem();
        } else {

selectedItem = mAdapter.getItem(position); // This line gets null if we return null from adapter

}
        if (selectedItem == null) {
            Log.w(TAG, "performCompletion: no selected item");
            return;
        }

 @Override
public Object getItem(int position) {
return null;
}

So the correct way is

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM