2012-09-05 23 views
3

我創建了一個活動,其中有textview/autocompletetextview,listview和一個按鈕。我的autocompletetextview效果很好,但我想將我在textview中輸入的內容編入索引視圖,當它突出顯示時,我會單擊按鈕並轉到相應的活動。Android AutocompleteTextView建議索引到列表視圖

這裏是迄今爲止我所做的事情:

String[] classes = { 
     "Acornsquash", 
     "Almonds", 
     "Apples", } 

private ListView lv; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_LEFT_ICON); 
    setContentView(R.layout.nutritional_list); 
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.nutrition32); 

    //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    //  android.R.layout.simple_dropdown_item_1line, classes); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list_item, classes);  

    AutoCompleteTextView tv = (AutoCompleteTextView) findViewById (R.id.txtSearcEngine); 

    tv.setThreshold(3); 
    tv.setAdapter(adapter); 
    tv.setTextColor(Color.BLACK); 

    lv = (ListView) findViewById(R.id.lvListSearch); 
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes)); 
    lv.setOnItemClickListener(this); 


public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

    lv.setSelection(position); 

    switch(position) 
    { 
    case 0: 
     Intent acorn = new Intent(Nutritional_List.this, AcornSquash.class); 
     startActivity(acorn); 
     break; 
    case 1: 
     Intent almonds = new Intent(Nutritional_List.this, Almonds.class); 
     startActivity(almonds); 
     break; 

我的問題是,我沒有一個線索如何索引建議從autocompletetextview到列表視圖做。任何幫助?謝謝。

回答

4

重寫

我張貼兩種方式來完成你想要的東西。我相信第一種方法編碼更簡單,使用起來更容易混淆。第二種方法是直接回答你的問題。


方法1
我發現你使用的是AutoCompleteTextView和一個ListView時,他們都提供相同的信息也好奇。 AutoCompleteTextView具有根據用戶文本過濾選區的優點,但可以通過添加簡單的EditText來過濾ListView本身,從而刪除AutoCompleteTextView和Button。

EditText edit = (EditText) findViewById(R.id.edit); 
edit.addTextChangedListener(new TextWatcher() { 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     adapter.getFilter().filter(s); 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
    public void afterTextChanged(Editable s) {} 
}); 

哪裏adapter就像是lvtv等類變量,並傳遞給lv適配器:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes); 
lv.setAdapter(adapter); 

方法2
做你問什麼最初,我們先將classes更改爲更強大的ArrayList<String>

ArrayList<String> classesList = new ArrayList<String>(Arrays.asList(classes)); 
... 

public void onCreate() { 
    ... 
    // Update both of your adapters! 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list_item, classesList);  

創建一個新的類變量int tvIndex = -1。我們將使用它,當我們從AutoCompleteTextView單擊某個項目:如果用戶按下按鈕

tv.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     String text = ((TextView) v).getText().toString(); 
     tvIndex = classesList.indexOf(text); 
     lv.setSelection(tvIndex); 
    } 
}); 

着手:

button.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if(tvIndex > -1) 
      lv.performItemClick(lv.getChildAt(tvIndex), tvIndex, tvIndex); 
    } 
}); 

希望幫助!

+0

你好,我試過你的代碼,但它不工作。 :( – Dunkey

+0

請看我上面更新的帖子^^我收錄了我的onItemClick方法 – Dunkey

+0

嗨,我要爲list,autoIndex和TextView v創建一個局部變量嗎? – Dunkey