Android 我有一个资源(JSON文件),其中包含从数据库获取数据以添加下拉列表中所需的所有数据
我对国家的答复:Android 我有一个资源(JSON文件),其中包含从数据库获取数据以添加下拉列表中所需的所有数据,android,android-intent,android-activity,android-spinner,Android,Android Intent,Android Activity,Android Spinner,我对国家的答复: {“searchvalues”:[{“id”:“1”,“name”:“india”},{“id”:“2”,“name”:“srilanka”}],“success”:1} 使用以下命令将国家名称添加到微调器:“countryadapter.add(jsonArray.getJSONObject(i).getString(“名称”);” 当我选择“coutry”时,请下拉“working fine”。例如,我选择“india”时,我希望从中选择“id”({“id”:“1”,“na
{“searchvalues”:[{“id”:“1”,“name”:“india”},{“id”:“2”,“name”:“srilanka”}],“success”:1}
“countryadapter.add(jsonArray.getJSONObject(i).getString(“名称”);”
中选择“id”({“id”:“1”,“name”:“india”)。
({“id”:“1”,“name”:“India”})获取所选项目id
country.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共无效项(AdapterView父项,
视图、整型位置、长id){
//TODO自动生成的方法存根
字符串countryname=country.getSelectedItem().toString();
StringRequest状态=新建StringRequest(Request.Method.POST、D_URL、new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsnobject=新的JSONObject(响应);
JSONArray JSONArray=jsnobject.getJSONArray(“searchvalues”);
distadapter.clear();
for(int i=0;i
当您通过单击项目微调器选择位置时
JSONObject object = new JSONObject("Your Response");
JSONArray countries = object.getJSONArray("searchvalues");
String id = countries.getJSONObject(position).getString("id");
通过这种方式,您可以获得ID验证JSON后,请尝试此方法
class Country
{
int id;
String name;
}
List<Country> countryList = new ArrayList<Country>();
country.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
String countryname = country.getSelectedItem().toString();
StringRequest state=new StringRequest(Request.Method.POST, D_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsnobject = new JSONObject(response);
JSONArray jsonArray =jsnobject.getJSONArray("searchvalues");
distadapter.clear();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj= jsonArray.getJSONObject(i);
Country tempCountry;
int id = obj.getInt("id");
String name = obj.getString("name");
tempCountry.id =id;
tempCountry.name = name;
countryList.add(tempCountry);
}
for(Country temp: countryList)
{
if(countryName.equals(temp.name))
Toast.makeText(getApplicationContext(),"id : "+temp.id,Toast.LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});
国家级
{
int-id;
字符串名;
}
List countryList=new ArrayList();
country.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共无效项(AdapterView父项,
视图、整型位置、长id){
//TODO自动生成的方法存根
字符串countryname=country.getSelectedItem().toString();
StringRequest状态=新建StringRequest(Request.Method.POST、D_URL、new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsnobject=新的JSONObject(响应);
JSONArray JSONArray=jsnobject.getJSONArray(“searchvalues”);
distadapter.clear();
for(int i=0;i
你只想在微调器中输入国家名称??你的最后一个要点不清楚。我不确定你到底在说什么。请详细说明。首先,你的Json无效。它应该是这样的{“countrys”:[{“id”:“1”,“name”:“India”},{“id”:“2”,“name”:“srilanka”}你可以用不同的方式来做1)简单方法:如果传递给微调器的数组中的位置no+1与id匹配,则在所选项目上,可以使用位置no+1(2)维护一个ArrayList countries;由于Country为replay@Amarbir Singh有id和name attributesq,我希望Country name下拉运行良好,当我选择印度时,我想从我的response({“id”:“1”,“name”:“india”})…guide metq@但它不适合我..我的json更改如下:{“searchvalues”:[{“id”:“1”,“name”:“india”},{“id”:“2”,“name”:“srilanka”}],“success”:1},例如,我的请求是:当im选择国家时,获取id发送到服务器,然后获取状态列表。。
class Country
{
int id;
String name;
}
List<Country> countryList = new ArrayList<Country>();
country.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
String countryname = country.getSelectedItem().toString();
StringRequest state=new StringRequest(Request.Method.POST, D_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsnobject = new JSONObject(response);
JSONArray jsonArray =jsnobject.getJSONArray("searchvalues");
distadapter.clear();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj= jsonArray.getJSONObject(i);
Country tempCountry;
int id = obj.getInt("id");
String name = obj.getString("name");
tempCountry.id =id;
tempCountry.name = name;
countryList.add(tempCountry);
}
for(Country temp: countryList)
{
if(countryName.equals(temp.name))
Toast.makeText(getApplicationContext(),"id : "+temp.id,Toast.LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});