2013-02-08 78 views
0

我在我的應用程序中使用AutoCompleteTextView,但它意外地工作。自動完成列表顯示列表中不想要的文本,但是當我選擇此文本並從autocomletetextview中刪除焦點時,它將自己的文本設置爲autocompletetext字段。另一個問題是,我想獲得autocompletetext字段中選定的文本的id我關聯這個id當我填充autocompletetextview的自定義適配器中的列表。 這是我使用的代碼。Android AutoCompleteTextView意外的行爲

autoComMarker = (AutoCompleteTextView) findViewById(R.id.auto_rainfall_of_markaz_id); 
autoComMarker.setThreshold(1); 
CustomMarkazAdapter adapter = new CustomMarkazAdapter(getBaseContext(),  R.layout.custom_auto_com_listview, marqazList); 
autoComMarker.setAdapter(adapter); 


autoComMarker.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> arg0, View view, 
       int position, long arg3) { 
      autoComMarker.setTag(view.getTag()); 
      Log.e("Markaz list", ""+view.getTag()); 
     } 
    }); 

這裏是CustomMarkazAdapter代碼:我正在根據autocompletelistview項目尋找,它集的Markaz ID

public class CustomMarkazAdapter extends ArrayAdapter<Markaz>{ 

private ArrayList<Markaz> items; 
private Context CurrentContext; 
private Markaz CurrentItem; 

public CustomMarkazAdapter(Context context, int textViewResourceId, 
     ArrayList<Markaz> objects) { 
    super(context, textViewResourceId, objects); 

    items = objects; 
    CurrentContext = context; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if ((items == null) || ((position + 1) > items.size())) 
     return convertView; //Can't extract item 

    CurrentItem = (Markaz)items.get(position);  

    LayoutInflater vi = (LayoutInflater)CurrentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = vi.inflate(R.layout.custom_auto_com_listview, null);  
    TextView text = (TextView) convertView.findViewById(R.id.custom_auto_com_text); 

    text.setText(CurrentItem.getMarkazName()); 
    convertView.setTag(CurrentItem.getMarkazID()); 
    Log.e(CurrentItem.getMarkazName(),""+CurrentItem.getMarkazID()); 
    return convertView; 
} 

}

的markazID我在這裏設置不position.which我不需要我需要markaz id我通過markazList傳遞。 請幫助我,任何幫助,將不勝感激。

回答

0

試試這個。

autoComMarker.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View view, 
        int position, long arg3) { 
       Markaz items= adapter.getItem(position); 
       //Now you can get items of Markaz from items.get... 
      } 
     }); 
+0

謝謝,它適用於我。 –

+0

隨時歡迎。實際上,您使用'marqazList'創建的模型內部適配器列表中填充了您的模型'Markaz'。 – user1621629

+0

如果您獲得解決方案,請接受答案。 – user1621629

0

你必須讓所選項目的positionlistview。在autoComMarker.setOnItemClickListener()中執行以下操作:

Markaz items= adapter.getItem(position);