Dunkey
Dunkey

Reputation: 1922

Android AutocompleteTextView suggestion to be indexed to listview

I'm creating an activity wherein there are textview/autocompletetextview, listview and a button. 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.

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. Any help? thanks.

Upvotes: 2

Views: 3077

Answers (1)

Sam
Sam

Reputation: 86948

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
I find it curious that you are using an AutoCompleteTextView and a ListView when they both provide the same information. 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.

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 = 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>:

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. We'll use it when we click an item from the 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!

Upvotes: 4

Related Questions