如何显示微调器以及每个微调器中的项目数

时间:2017-04-10 11:53:35

标签: android android-layout count spinner

我有2个微调器.1个微调器显示城市,第2个微调器显示基于微调器1上选择的城市的区域。 例如,如果孟买市有5个区域......我的第一个旋转器应该显示孟买(5),同样也适用于其他城市。 这是我的代码:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


public class SpinnerActivity extends Activity implements OnItemSelectedListener  {



    Spinner spinnerCity;
   Spinner spinnerArea;
    AutoCompleteTextView auto_search;
    String city[];
    ImageView img_search_button;
    String count;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        spinnerCity = (Spinner) findViewById(R.id.spinnerCity);

        // spinnerArea = (Spinner) findViewById(R.id.spinnerArea);
        ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(this, R.array.city_array, android.R.layout.simple_spinner_item);
        countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinnerCity.setAdapter(countryAdapter);

         spinnerCity.setOnItemSelectedListener(this);



        auto_search = (AutoCompleteTextView) findViewById(R.id.auto_search);
        img_search_button = (ImageView) findViewById(R.id.img_search_button);
        // autoSearch Adapter
        city = getResources().getStringArray(R.array.city_array);
//test 19 jan
        int j = 0;

        for (int i = 0; i < city.length; i++) {

            String item = city[i];
            if (item instanceof CharSequence) {
                //text.setText((CharSequence)item);
                j++;

            } else {

            }
        }
        String outletsTemp[] = new String[j];
        j = 0;
        for (int i = 0; i < city.length; i++) {

            String item = city[i];
            if (item instanceof CharSequence) {
                //text.setText((CharSequence)item);
                outletsTemp[j] = item;
                j++;
                System.out.println(item);
            } else {
                //text.setText(item.toString());
                System.out.println(item + "test");
                //outletsTemp[i] = "test";
            }
        }
        city = new String[outletsTemp.length];
        city = outletsTemp;


        ArrayAdapter<CharSequence> outletList = new ArrayAdapter<CharSequence>(this, R.layout.spinner_search, city);
        outletList.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        //AutoCompleteAdapter  outletList = new AutoCompleteAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,modelData.outletList);
        auto_search.setAdapter(outletList);
        auto_search.setThreshold(1);

        auto_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                String selection = (String) parent.getItemAtPosition(position);
                int loc = -1;

                for (int i = 0; i < city.length; i++) {
                    if (city[i].equals(selection)) {
                        loc = i;
                        break;
                    }
                }
                System.out.println("Position " + loc);
               /* modelData.shopPosition = pos;
                spinner_outlets.setSelection(modelData.shopPosition);*/
                spinnerCity.setSelection(loc);
                auto_search.setVisibility(View.GONE);
                spinnerCity.setVisibility(View.VISIBLE);

                /*InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);*/


            }
        });
        auto_search.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;


                if (event.getRawX() >= (auto_search.getRight() - auto_search.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    // your action here
                    auto_search.setText("");
                    return true;
                }

                return false;
            }
        });
        // autoSearch END


        img_search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (spinnerCity.getVisibility() == View.VISIBLE) {
                    auto_search.setVisibility(View.VISIBLE);
                    spinnerCity.setVisibility(View.GONE);
                } else {
                    auto_search.setVisibility(View.GONE);
                    spinnerCity.setVisibility(View.VISIBLE);
                }


            }
        });
        //Intent intent1 = getIntent();

       // count= intent1.getStringExtra("COUNT").toString();
    }

    @Override

    public void onItemSelected(AdapterView<?> parent, View arg1, int pos,

                               long arg3) {
//int num=parent.getCount();
       String value= parent.getItemAtPosition(pos).toString();

        if (value.equals("Chennai")) {


            Intent intent = new Intent(this, Spin2Activity.class);
            intent.putExtra("VALUE", value);
            startActivity(intent);
        } else if (value.equals("Mumbai")) {


            Intent intent = new Intent(this, Spin2Activity.class);
            intent.putExtra("VALUE", value);
            startActivity(intent);

        } else if (value.equals("Calcutta")) {

            Intent intent = new Intent(this, Spin2Activity.class);
            intent.putExtra("VALUE", value);
            startActivity(intent);

        }

    }



    @Override

    public void onNothingSelected(AdapterView<?> arg0) {

    }

Spinner2Activity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class Spin2Activity extends AppCompatActivity{
    Spinner spinnerArea;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spin2);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
        Intent intent = getIntent();

        String value= intent.getStringExtra("VALUE");
        spinnerArea = (Spinner) findViewById(R.id.spinnerArea);
        if (value.equals("Chennai")) {


            ArrayAdapter<CharSequence> adapter = ArrayAdapter

                    .createFromResource(this, R.array.area_chennai,

                            android.R.layout.simple_spinner_dropdown_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerArea.setAdapter(adapter);
            // int count= spinnerArea.getAdapter().getCount();
            Toast.makeText(getApplicationContext(), "Total number of Items are:" + spinnerArea.getAdapter().getCount(), Toast.LENGTH_LONG).show();


        }else if (value.equals("Mumbai")){
            ArrayAdapter<CharSequence> adapter = ArrayAdapter

                    .createFromResource(this, R.array.area_mumbai,

                            android.R.layout.simple_spinner_dropdown_item);



            spinnerArea.setAdapter(adapter);

            // int count= spinnerArea.getAdapter().getCount();
            Toast.makeText(getApplicationContext(), "Total number of Items are:" + spinnerArea.getAdapter().getCount(), Toast.LENGTH_LONG).show();
        }else if(value.equals("Calcutta")){
            ArrayAdapter<CharSequence> adapter = ArrayAdapter

                    .createFromResource(this, R.array.area_calcutta,

                            android.R.layout.simple_spinner_dropdown_item);


            spinnerArea.setAdapter(adapter);
        }

}

}

我的spinner1的xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal">
        <Spinner

            android:id="@+id/spinnerCity"
            style="?android:attr/spinnerItemStyle"
            android:layout_width="200dp"
            android:spinnerMode="dialog"
            android:layout_height="60dp"

            android:layout_weight="3"
            android:layout_centerHorizontal="true"

            android:layout_marginTop="8dp"

            />
       <!-- <ListView xmlns:android="http://schemas.android.com/apk/res/android"

            android:id="@+id/spinnerCity"
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:layout_weight="3"
            android:layout_marginTop="8dp"
            />-->

        <AutoCompleteTextView android:id="@+id/auto_search" android:visibility="gone"
            android:layout_width="10dp"
            android:layout_height="60dp" android:textSize="20dp"
            android:layout_weight="3"  android:drawableRight="@android:drawable/ic_delete"/>
        <ImageView
            android:id="@+id/img_search_button"
            android:layout_marginTop="8dp"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="3"
            android:src="@android:drawable/ic_search_category_default" android:padding="3dp"/>
    </LinearLayout>


</LinearLayout>

我的字符串值::

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Spinner</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="Login">Login</string>

    <string-array name="city_array">
        <item>Delhi</item>
        <item>Chennai</item>
        <item>Mumbai</item>
        <item>Calcutta</item>
    </string-array>
    <string-array name="area_chennai">
        <item>T Nagar</item>
        <item>Mount Road</item>
        <item>Mylapore</item>
        <!--<item>Vadapalani</item>-->
    </string-array>
    <string-array name="area_mumbai">
        <item>Andheri</item>
        <item>Bandra</item>
        <item>Dadar</item>
        <item>Matunga</item>
    </string-array>
    <string-array name="area_calcutta">
        <item>Barasat</item>
        <item>Dum Dum</item>
        <item>Kanchrapara</item>
        <item>Titagarh</item>
    </string-array>


    <string name="title_activity_login">App</string>
    <string name="title_activity_spin2">Spin2Activity</string>

</resources>

最后告诉我如何显示微调器以及每个微调器中的项目数

  

输出:    spinner1应显示在下拉列表中

     

孟买(5)加尔各答(4)钦奈(4)

     

然后微调器2显示每个spinner1项目中的项目

1 个答案:

答案 0 :(得分:1)

您可以使用包含文本和计数的布局编写自定义数组适配器,并从自定义适配器中的getDropDownView方法填充这两者。