Android 在Webservices的帮助下从Spinner选择客户名称时,从数据库检索特定的客户ID?

Android 在Webservices的帮助下从Spinner选择客户名称时,从数据库检索特定的客户ID?,android,api,web-services,spinner,android-spinner,Android,Api,Web Services,Spinner,Android Spinner,当我从微调器中选择客户名称时,如何检索特定的客户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

当我从微调器中选择客户名称时,如何检索特定的客户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);

在这里,您可以在一个列表中获得相应的客户ID,还可以获得每个微调器值的位置。 如果客户名单是

List<String> cus_list={ "Anitha", "Devi" , "Suja","Puppy","Kishri"};
试试这个

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

            }
        });
sp_lv_Receiptedit_lv_Receiptedit_Fees_Name.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener()){
@凌驾
已选择公共位置(适配器视图适配器视图视图视图视图内部位置长l){
String itemValue=(String)sp_lv_Receiptedit_lv_receipt_Fees_Name.getItemAtPosition(position);
字符串customerId=customerIDs.get(位置);
Log.i(“客户详细信息”、“客户详细信息”+itemValue+customerId);
Toast.makeText(MainActivity.this,“客户名称”+itemValue+“客户ID”+customerId,Toast.LENGTH_LONG).show();
}
@凌驾
未选择公共无效(AdapterView AdapterView){
}
});
输出

所选名称ID


这里我向ArrayList添加虚拟值,并在微调器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) {

    }
});
customerNames.add(“安妮莎”);
客户名称。添加(“Devi”);
客户名称。添加(“Suja”);
客户ID。添加(“3”);
客户ID。添加(“5”);
客户ID。添加(“8”);
sp.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_spinner_dropdown_项目,customerNames));
sp.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Log.e(“customer_id”,customerIDs.get(position));
}
@凌驾
未选择公共无效(AdapterView父级){
}
});

此日志返回相应的id

请添加您的json响应以填充spinner。我建议您缩小您的问题范围,并具体说明您想要什么do@Tej-我需要在从微调器单击客户名称时检索客户ID?System.out:打印学号:[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]@MeenaRengarajan blueGem您还没有添加json响应。添加您的json响应,以获得获取客户id的想法。学校id是客户id。有人可以建议此问题吗?@MeenaRengarajan blueGem请检查我的ans,如果您有任何问题,请告诉我。@MeenaRengarajan blueGem谢谢,很高兴帮助您。感谢您的帮助!
List<String> cus_id_list={ "12", "3" , "6","43","21"};
String id = cus_id_list.get[1]
 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) {

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

    }
});