如何在Android Spinner中添加带值的名称(两项)

如何在Android Spinner中添加带值的名称(两项),android,Android,我是android开发的新手。我想在spinner中填入各州的名称及其各自的值,例如:- 值|状态名称 |256哈里亚纳邦| |895旁遮普省| |987德里| |893卡拉| 我希望在微调器中显示状态名称,每当我从微调器中选择状态名称时,我希望获得状态名称的相应值,就像在Asp.net下拉列表中一样。 可能吗? 请帮忙! 提前谢谢 这些将对您有所帮助 MainActivity.java: import java.util.ArrayList; import org.apache.http.H

我是android开发的新手。我想在spinner中填入各州的名称及其各自的值,例如:-

值|状态名称 |256哈里亚纳邦| |895旁遮普省| |987德里| |893卡拉|

我希望在微调器中显示状态名称,每当我从微调器中选择状态名称时,我希望获得状态名称的相应值,就像在Asp.net下拉列表中一样。 可能吗? 请帮忙!
提前谢谢

这些将对您有所帮助

MainActivity.java:

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ProgressDialog mProgressDialog;
    ArrayList<String> worldlist;
    ArrayList<WorldPopulation> world;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new DownloadJSON().execute();
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void> 
        {

            @Override
            protected Void doInBackground(Void... params) {
                 world = new ArrayList<WorldPopulation>();
                 worldlist = new ArrayList<String>();
            try
            {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpGet request = new HttpGet("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();

            String _response=EntityUtils.toString(resEntity); 
            Log.i("Response is......................",""+_response);
            jsonobject = new JSONObject(_response); 
            jsonarray = jsonobject.getJSONArray("worldpopulation");
                for (int i = 0; i < jsonarray.length(); i++) {
                    jsonobject = jsonarray.getJSONObject(i);

                    WorldPopulation worldpop = new WorldPopulation();

                    worldpop.setRank(jsonobject.optString("rank"));
                    worldpop.setCountry(jsonobject.optString("country"));
                    worldpop.setPopulation(jsonobject.optString("population"));
                    worldpop.setFlag(jsonobject.optString("flag"));
                    world.add(worldpop);

                    worldlist.add(jsonobject.optString("country"));  
                }
            }catch(Exception e)
            {

            }
            return null;

            }
            protected void onPostExecute(Void args) {
                Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);

                mySpinner
                        .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                                android.R.layout.simple_spinner_dropdown_item,
                                worldlist));

                mySpinner
                        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int position, long arg3) {

                                TextView txtrank = (TextView) findViewById(R.id.rank);
                                TextView txtcountry = (TextView) findViewById(R.id.country);
                                TextView txtpopulation = (TextView) findViewById(R.id.population);

                                txtrank.setText("Rank : "
                                        + world.get(position).getRank());
                                txtcountry.setText("Country : "
                                        + world.get(position).getCountry());
                                txtpopulation.setText("Population : "
                                        + world.get(position).getPopulation());
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                            }
                        });
            }
       } 

}
public class WorldPopulation {


        String rank,country,population,flag;

        public String getRank() {
            return rank;
        }

        public void setRank(String rank) {
            this.rank = rank;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getPopulation() {
            return population;
        }

        public void setPopulation(String population) {
            this.population = population;
        }

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }
    }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple" >

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:popupBackground="#DB3535"

         />

    <TextView
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_spinner" />

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rank" />

    <TextView
        android:id="@+id/population"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/country" />

</RelativeLayout>
活动\u main.xml:

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ProgressDialog mProgressDialog;
    ArrayList<String> worldlist;
    ArrayList<WorldPopulation> world;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new DownloadJSON().execute();
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void> 
        {

            @Override
            protected Void doInBackground(Void... params) {
                 world = new ArrayList<WorldPopulation>();
                 worldlist = new ArrayList<String>();
            try
            {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpGet request = new HttpGet("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();

            String _response=EntityUtils.toString(resEntity); 
            Log.i("Response is......................",""+_response);
            jsonobject = new JSONObject(_response); 
            jsonarray = jsonobject.getJSONArray("worldpopulation");
                for (int i = 0; i < jsonarray.length(); i++) {
                    jsonobject = jsonarray.getJSONObject(i);

                    WorldPopulation worldpop = new WorldPopulation();

                    worldpop.setRank(jsonobject.optString("rank"));
                    worldpop.setCountry(jsonobject.optString("country"));
                    worldpop.setPopulation(jsonobject.optString("population"));
                    worldpop.setFlag(jsonobject.optString("flag"));
                    world.add(worldpop);

                    worldlist.add(jsonobject.optString("country"));  
                }
            }catch(Exception e)
            {

            }
            return null;

            }
            protected void onPostExecute(Void args) {
                Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);

                mySpinner
                        .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                                android.R.layout.simple_spinner_dropdown_item,
                                worldlist));

                mySpinner
                        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int position, long arg3) {

                                TextView txtrank = (TextView) findViewById(R.id.rank);
                                TextView txtcountry = (TextView) findViewById(R.id.country);
                                TextView txtpopulation = (TextView) findViewById(R.id.population);

                                txtrank.setText("Rank : "
                                        + world.get(position).getRank());
                                txtcountry.setText("Country : "
                                        + world.get(position).getCountry());
                                txtpopulation.setText("Population : "
                                        + world.get(position).getPopulation());
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                            }
                        });
            }
       } 

}
public class WorldPopulation {


        String rank,country,population,flag;

        public String getRank() {
            return rank;
        }

        public void setRank(String rank) {
            this.rank = rank;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getPopulation() {
            return population;
        }

        public void setPopulation(String population) {
            this.population = population;
        }

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }
    }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple" >

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:popupBackground="#DB3535"

         />

    <TextView
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_spinner" />

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rank" />

    <TextView
        android:id="@+id/population"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/country" />

</RelativeLayout>

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#88CCDB</color>
</resources>

#88CCDB
输出:

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    JSONObject jsonobject;
    JSONArray jsonarray;
    ProgressDialog mProgressDialog;
    ArrayList<String> worldlist;
    ArrayList<WorldPopulation> world;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new DownloadJSON().execute();
    }

    private class DownloadJSON extends AsyncTask<Void, Void, Void> 
        {

            @Override
            protected Void doInBackground(Void... params) {
                 world = new ArrayList<WorldPopulation>();
                 worldlist = new ArrayList<String>();
            try
            {
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpGet request = new HttpGet("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");
            HttpResponse response = httpclient.execute(request);
            HttpEntity resEntity = response.getEntity();

            String _response=EntityUtils.toString(resEntity); 
            Log.i("Response is......................",""+_response);
            jsonobject = new JSONObject(_response); 
            jsonarray = jsonobject.getJSONArray("worldpopulation");
                for (int i = 0; i < jsonarray.length(); i++) {
                    jsonobject = jsonarray.getJSONObject(i);

                    WorldPopulation worldpop = new WorldPopulation();

                    worldpop.setRank(jsonobject.optString("rank"));
                    worldpop.setCountry(jsonobject.optString("country"));
                    worldpop.setPopulation(jsonobject.optString("population"));
                    worldpop.setFlag(jsonobject.optString("flag"));
                    world.add(worldpop);

                    worldlist.add(jsonobject.optString("country"));  
                }
            }catch(Exception e)
            {

            }
            return null;

            }
            protected void onPostExecute(Void args) {
                Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);

                mySpinner
                        .setAdapter(new ArrayAdapter<String>(MainActivity.this,
                                android.R.layout.simple_spinner_dropdown_item,
                                worldlist));

                mySpinner
                        .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int position, long arg3) {

                                TextView txtrank = (TextView) findViewById(R.id.rank);
                                TextView txtcountry = (TextView) findViewById(R.id.country);
                                TextView txtpopulation = (TextView) findViewById(R.id.population);

                                txtrank.setText("Rank : "
                                        + world.get(position).getRank());
                                txtcountry.setText("Country : "
                                        + world.get(position).getCountry());
                                txtpopulation.setText("Population : "
                                        + world.get(position).getPopulation());
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                            }
                        });
            }
       } 

}
public class WorldPopulation {


        String rank,country,population,flag;

        public String getRank() {
            return rank;
        }

        public void setRank(String rank) {
            this.rank = rank;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getPopulation() {
            return population;
        }

        public void setPopulation(String population) {
            this.population = population;
        }

        public String getFlag() {
            return flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }
    }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple" >

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:popupBackground="#DB3535"

         />

    <TextView
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_spinner" />

    <TextView
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rank" />

    <TextView
        android:id="@+id/population"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/country" />

</RelativeLayout>