简体   繁体   English

Android AutocompleteTextView建议被索引到ListView

[英]Android AutocompleteTextView suggestion to be indexed to listview

I'm creating an activity wherein there are textview/autocompletetextview, listview and a button. 我正在创建一个活动,其中有textview / autocompletetextview,listview和一个按钮。 My autocompletetextview works well, but I want to index what I input in the textview to the listview and when the it is highlighted, I'd click the button and go to the corresponding activity. 我的autocompletetextview效果很好,但是我想将在textview中输入的内容索引到listview,并在突出显示该内容时,单击按钮并转到相应的活动。

Here's so far what I've done: 到目前为止,这是我所做的:

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;

My problem is, I don't have a clue on how to index suggestion from autocompletetextview to listview. 我的问题是,我不知道如何将建议从autocompletetextview索引到listview。 Any help? 有什么帮助吗? thanks. 谢谢。

Rewrite 改写

I am posting two ways to accomplish what you want. 我发布了两种方法来实现您想要的。 I believe that first method is simpler to code and less confusing to use. 我相信第一种方法更容易编写代码,使用起来也更容易混淆。 The second method is the direct answer to your question. 第二种方法是直接回答您的问题。


Method 1 方法1
I find it curious that you are using an AutoCompleteTextView and a ListView when they both provide the same information. 我感到奇怪的是,当您同时使用AutoCompleteTextView和ListView时,它们都提供相同的信息。 The AutoCompleteTextView has the advantage of filtering the selection based on the user's text, but you can remove the AutoCompleteTextView and Button by using adding simple EditText that filters the ListView itself. 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) {}
});

Where adapter is a class variable like lv , tv , etc. and is the adapter passed to lv : 其中adapter是类变量,例如lvtv等,并且是传递给lv的适配器:

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

Method 2 方法二
To do what you asked about initially, first let's change classes to a more powerful ArrayList<String> : 要执行您最初提出的要求,首先让我们将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 );    

create a new class variable as int tvIndex = -1 . 创建一个新的类变量,如int tvIndex = -1 We'll use it when we click an item from the AutoCompleteTextView: 单击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);
    }
});

If the user pushes the Button to proceed: 如果用户按下按钮继续:

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

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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