2014-10-06 95 views
3

自動完成文本正確列出一切工作正常..但問題是一旦我點擊自動完成項目,它不選擇。「沒有選定的項目」雖然AutoCompleteTextview項目點擊

當我點擊物品時,Logcat顯示「No Selected Item」。

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. 
     } 
    }); 

// --------------適配器類

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(); 
} 

}

是否有人在這些類型的問題來了?

+0

沒明白這裏的一切......是什麼你的適配器類?你的父級佈局/主視圖的其他元素上是否還有其他Listener? – mithrop 2014-10-06 12:19:18

+0

我使用autocompletetextview來顯示基於國家的位置。基於搜索字符串,它正在從服務器加載正確的值。但問題是,當我選擇一個項目時,它沒有做任何事......並且logcat顯示爲「沒有選定的項目「 – 2014-10-06 12:22:09

+0

好的。什麼是你設置OnItemClickListener的'Autocomplte'實例? – mithrop 2014-10-06 12:24:16

回答

4

最後我得到了解決方案;

由於autocompleteTextview,它使用adapter.getItem(位置),使用方法 「performCompletion」 -----得到值指android.widget.AutoCompleteTextview

樣品:

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

將selectedItem = mAdapter.getItem(position); //此行變得無效,如果我們從適配器返回null

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

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

所以,正確的做法是

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

非常感謝!我想知道爲什麼onItemClickListener根本沒有被調用。 'getItem()'必須用非空結果實現! – yuku 2015-10-28 16:07:54