如何在从微调器中选择客户名称时检索特定客户ID?
我的代码是,
public void parseJsonResponse_Edit_Lv_Receipt_Details(String result) {
Log.i(TAG, result);
// you can use this array to find the school ID based on name
ArrayList<SpinnerClass_ITEMS> customers = new ArrayList<SpinnerClass_ITEMS>();
// you can use this array to populate your spinner
ArrayList<String> customerNames = new ArrayList<String>();
ArrayList<String> customerIDs = new ArrayList<String>();
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = new JSONArray(json.getString("customer"));
Log.i(Edit_Lv_Receipt_Details.class.getName(),
"Number of entries " + jArray.length());
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
System.out.println("Check json object in View CUstomersList:" +jObject);
String json_id=jObject.getString("id");
System.out.println("Customer ID is:"+json_id);
SpinnerClass_ITEMS customer= new SpinnerClass_ITEMS();
customer.setId(jObject.getString("id"));
System.out.println("Check ID:"+customer);
customer.setName(jObject.getString("name"));
System.out.println("Check Name:"+customer);
customers.add(customer);
获取客户名称即。从数据库中检索客户名称并在spinner中显示:
customerNames.add(jObject.optString("name"));
System.out.println("Print customerNames:"+customerNames);
**//Getting Customer IDS ie. Retrieving the customer IDS from a Database and storing it in arraylist//**
customerIDs.add(jObject.optString("id"));
System.out.println("Print customerIDS:"+customerIDs);
}
} catch (JSONException e) {
e.printStackTrace();
}
sp_lv_Receiptedit_lv_receipt_Fees_Name.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, customerNames));
**/*Selecting spinner position and corresponding values and displaying with the help of toasts*/**
sp_lv_Receiptedit_lv_receipt_Fees_Name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) sp_lv_Receiptedit_lv_receipt_Fees_Name.getItemAtPosition(position);
int itemPosition = position;
Hold = sp_lv_Receiptedit_lv_receipt_Fees_Name.getSelectedItemPosition() + 1 ;
Toast.makeText(Edit_Lv_Receipt_Details.this,"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
在上面的代码中,我在webservices的帮助下填充了微调器内的数据。
在Logcat中,
在这里,打印客户姓名: 打印客户名称:[Pooja,Rakesh,saranya,Devikaa,meenakshi,Meena,Rengarajan,Rengarajan,Meena,Meena,Meena,akila,客户,Meena,Meena,Meena,Meena,Laila,Susila,Manu,Rengarajan,Ravi,Meena ,Prabhu,Prabha,Zealoit,ZealoitVijay,Leo,prabha]
此处,通过在webservice URL:
的帮助下存储在arraylist中来打印客户IDSystem.out:打印客户IDS:[1,2,8,9,10,11,12,13,14,15,16,18,20,21,22,23,24,25,26, 27,28,29,30,31,32,33,34,35,37]
但我需要的是,当我从微调器中选择客户名称时需要检索特定的客户ID吗?
在选择微调器值时,如何获取特定选定客户名称的客户ID(检查Json响应)?
JSON响应是:
{
"customer": [{
"id": "1",
"name": "Pooja"
}, {
"id": "2",
"name": "Rakesh"
}, {
"id": "8",
"name": "saranya"
}, {
"id": "9",
"name": "Devikaa"
}, {
"id": "10",
"name": "meenakshi"
}, {
"id": "11",
"name": "Meena"
}, {
"id": "12",
"name": "Rengarajan"
}, {
"id": "13",
"name": "Rengarajan"
}, {
"id": "14",
"name": "Meena"
}, {
"id": "15",
"name": "Meena"
}, {
"id": "16",
"name": "Meena"
}, {
"id": "18",
"name": "akila"
}, {
"id": "20",
"name": "customer"
}, {
"id": "21",
"name": "Meena"
}, {
"id": "22",
"name": "Meena"
}, {
"id": "23",
"name": "Meena"
}, {
"id": "24",
"name": "Meena"
}, {
"id": "25",
"name": "Laila"
}, {
"id": "26",
"name": "Susila"
}, {
"id": "27",
"name": "Manu"
}, {
"id": "28",
"name": "Rengarajan"
}, {
"id": "29",
"name": "Ravi"
}, {
"id": "30",
"name": "Meena"
}, {
"id": "31",
"name": "Prabhu"
}, {
"id": "32",
"name": "Prabha"
}, {
"id": "33",
"name": "Zealoit"
}, {
"id": "34",
"name": "ZealoitVijay"
}, {
"id": "35",
"name": "Leo"
}, {
"id": "37",
"name": "prabha"
}]}
答案 0 :(得分:3)
这里我将虚拟值添加到ArrayList并在spinner中检查它setOnItemSelectedListener
customerNames.add("Anitha");
customerNames.add("Devi");
customerNames.add("Suja");
customerIDs.add("3");
customerIDs.add("5");
customerIDs.add("8");
sp.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, customerNames));
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("customer_id", customerIDs.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
此日志返回相应的ID
答案 1 :(得分:0)
在这里,您可以在一个列表中获得相应的客户ID,并且您还拥有每个微调器值的位置。 如果客户列表是
List<String> cus_list={ "Anitha", "Devi" , "Suja","Puppy","Kishri"};
因此,如果我们选择Devi postion是1
和客户ID列表是
List<String> cus_id_list={ "12", "3" , "6","43","21"};
列表的大小是相同的。所以这里我们有微调器的位置,它将等于列表的位置。
所以,我们可以从cus_is_list中获取
String id = cus_id_list.get[1]
答案 2 :(得分:0)
试试这个
sp_lv_Receiptedit_lv_receipt_Fees_Name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String itemValue = (String) sp_lv_Receiptedit_lv_receipt_Fees_Name.getItemAtPosition(position);
String customerId = customerIDs.get(position);
Log.i("Customer Details","Customer Details "+ itemValue + customerId);
Toast.makeText(MainActivity.this,"Customer Name "+ itemValue + " Customer ID " + customerId ,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
<强>输出强>
所选名称ID