1
  1. 我正在做一个小型 Android 应用程序。
  2. 因为我有一个带有物品清单的微调器(前品牌,颜色)。
  3. 而且我还有 MultiAutoCompleteTextView。
  4. 我创建了两个自定义阵列适配器颜色适配器和品牌适配器。
  5. 颜色适配器保存与颜色相关的值,品牌适配器保存与品牌相关的值。
  6. 在这里,我要做的是验证微调器中的选定项目,例如如果我在微调器中选择颜色,然后在 MultiAutoCompleteTextView 中,它应该给我颜色适配器中的值。如果我选择 Brand In Spinner 然后 Values In Brand 适配器。
  7. 谁能帮我锄头做这个...
  8. 这是我的代码到目前为止我尝试过的内容。

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. 任何帮助表示赞赏谢谢.......
4

1 回答 1

0

尝试这个,

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) {

        }
于 2016-04-12T06:56:11.697 回答