2012-09-05 22 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