gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:48 24 4
gpt4 key购买 nike

我是 android 开发的新手。我想用状态名称及其各自的值填充微调器,例如:-

值 |州名

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

}

WorldPopulation.java:

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;
}
}

activity_main.xml:

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

输出:

enter image description here

enter image description here

关于android - 如何在 Android Spinner 中添加带有值(两项)的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22556521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com