在 Web 服务的帮助下从 Spinner 选择客户名称的同时从数据库中检索特定的客户 ID?

Retrieve the particular customer ID from a Database while selecting a customer name from Spinner with the help of Webservices?

如何在我从微调器select输入客户名称时检索特定的客户 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);

获取客户​​名称,即。从数据库中检索客户名称并将其显示在微调器中:

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

        }
    });

在上面的代码中,我在 web 服务的帮助下填充微调器中的数据。

在Logcat,

在这里,打印客户姓名: 印刷客户姓名:[Pooja, Rakesh, saranya, Devikaa, meenakshi, Meena, Rengarajan, Rengarajan, Meena, Meena, Meena, akila, customer, Meena, Meena, Meena, Meena, Laila, Susila, Manu, Rengarajan, Ravi, Meena , Prabhu, Prabha, Zealoit, ZealoitVijay, Leo, prabha]

此处,借助 webservice URL :

通过存储在数组列表中来打印客户 ID

System.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]

但我需要的是,当我 select 从微调器中获取客户名称时需要检索特定的客户 ID?

如何在我 select 微调器值时获取特定 select 编辑的客户名称的客户 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"
}]}

在这里,您将在一个列表中获得相应的客户 ID,并且您还拥有每个微调器值的位置。 如果客户列表是

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

所以如果我们 select Devi 位置是 1

客户 ID 列表是

List<String> cus_id_list={ "12", "3" , "6","43","21"};

列表的大小将是 same.So 这里我们有微调器的位置,它将等于列表的位置。

所以,我们可以从 cus_is_list 得到像

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

            }
        });

输出

选择的名称 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) {

    }
});

本日志returns对应id