androidandroid-spinnermultiautocompletetextview

Android Spinner With MultiAutoCompleteTextView?


  1. I'm Doing A Small Android App.
  2. In that I Have A Spinner With List Of Items(ex-Brand,Color).
  3. And Also I Have MultiAutoCompleteTextView.
  4. And I created Two Custom Array Adapters Color adapter and Brand Adapter.
  5. Color Adapter Holds The Values related to color and brand adapter holds values related to brand.
  6. Here What I'm Trying to do is validating The Selected Item in spinner like If I select Color In Spinner Then In MultiAutoCompleteTextView it should give me values in color adapter. If I select Brand In Spinner Then Values In Brand adapter.
  7. Can Anyone Help Me Hoe To Do This...
  8. Here Is My Code What I tried Up to now.

package com.example.ns2.testing;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {
    String[] color={"Blue","Black","Red","Yellow","Green","Pink","Sony","LG","Samsung"};
    String[] brand={"Sony","LG","Samsung"};

    MultiAutoCompleteTextView text1;
    Spinner spinner;
    ArrayAdapter<CharSequence> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text1= (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
        ArrayAdapter clistAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, color);
        ArrayAdapter blistAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, brand);

        text1.setAdapter(clistAdapter);
        text1.setAdapter(blistAdapter);
        text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        spinner = (Spinner) findViewById(R.id.spinner);
        adapter = ArrayAdapter.createFromResource(this, R.array.SearchKey, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String text = spinner.getSelectedItem().toString();
                Log.d("!!!!!!!!!!!!", "onItemSelected: "+text);



            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }






   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }*/

    /*@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }*/
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.ns2.testing.MainActivity"
    tools:showIn="@layout/activity_main">
    <TextView
        android:id="@+id/like"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/like"
        android:textSize="@dimen/abc_action_bar_progress_bar_size"
        android:layout_alignParentLeft="true"
        />



    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/like"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="70dp"
        android:layout_marginEnd="200dp">
    </Spinner>

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Value.... "
        android:layout_alignTop="@+id/spinner"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

  1. Any Help Appreciated Thanks.......

Solution

  • Try this,

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String text = spinner.getSelectedItem().toString();
                Log.d("!!!!!!!!!!!!", "onItemSelected: "+text);
                if(text.equals("Color")){
                     text1.setAdapter(clistAdapter);
                }  else if(text.equals("Brand")){
                     text1.setAdapter(blistAdapter);
                }  
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }