如何从android中的spinner获取选择的代码和选定的名称值

时间:2022-12-03 10:09:36

this is my Asynk task am getting data from server and calling in Oncreate method

这是我的Asynk任务从服务器获取数据并在Oncreate方法中调用

   class download extends AsyncTask<Void, Void, Void> {
            String result;

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();

                pDialog = new ProgressDialog(RDistributor.this);
                pDialog.setTitle("CheckUser Connection");
                pDialog.setMessage("Please wait...");
                pDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub

                try {
                    result = JSONfunctions2.getJSONfromURL(URL + "?spokecode="
                            + "GUJAHM270");
                    _jarray1 = new JSONArray(result);

                    stringArray = new ArrayList<String>();
                    // stringArray.add("Select Role");
                    for (int i = 0; i <= _jarray1.length(); i++) {

                        JSONObject _obj = _jarray1.getJSONObject(i);
                        SpokeCode = _obj.getString("SpokeCode").toString();
                        DistributerCode = _obj.getString("DistributerCode")
                                .toString();
                        DistributerName = _obj.getString("DistributerName")
                                .toString();

                        stringArray.add(DistributerName);

                    }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub

                if (_jarray1.length() == 0) {
                    showpopup("All Distributers had already been registered!");

                } else {
                    dataAdapter = new ArrayAdapter<String>(RDistributor.this,
                            R.layout.spinner_item, stringArray);
                    // ArrayAdapter<String> dataAdapter = new
                    // ArrayAdapter<String>(this,R.layout.spinner_item,
                    // stringArray);
                    dataAdapter
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    // set the ArrayAdapter to the spinner
                    rolespinner.setAdapter(dataAdapter);
                }
                pDialog.dismiss();
                super.onPostExecute(result);

            }
        }

this is itemselected of spinner and getting value .

这是微调器和获取值的项目。

rolespinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

                DistributerName=rolespinner.getSelectedItem().toString();
                Toast.makeText(RDistributor.this, DistributerName, 10000).show();


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

i am printing data distributerName in Spinner which is coming from server also distributerCode and spoke code coming from server i want when i select distributor name corresponding i get value of distributor code and spoke code selection of ditributorname from drop down please suggest me how i will achieve this .i am facing issue to get this .

我在Spinner中打印数据distributerName来自服务器也是来自服务器的分配器代码和来自服务器的代码我希望当我选择分销商名称对应我得到分销商代码的价值和ditributorname的代码选择从下拉请告诉我我将如何实现这个。我正面临着这个问题。

2 个解决方案

#1


Declare :

 HashMap<String, String> spinnerMap = new HashMap<String, String>();

for (int i = 0; i <= _jarray1.length(); i++) {

                    JSONObject _obj = _jarray1.getJSONObject(i);
                    SpokeCode = _obj.getString("SpokeCode").toString();
                    DistributerCode = _obj.getString("DistributerCode")
                            .toString();
                    DistributerName = _obj.getString("DistributerName")
                            .toString();

                    spinnerMap.put(DistributerName, DistributerCode);
                    stringArray.add(DistributerName);

                }

and spinner item-select Method update this :

和spinner item-select方法更新:

rolespinner.setOnItemSelectedListener(new OnItemSelectedListener() {

rolespinner.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub

        String to=  spinnerMap.get(rolespinner.getSelectedItem());
        Toast.makeText(MainActivity.this,
            to, 10000);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

enjoy!!!

#2


I think you need to wrap a object for storing these property.

我认为你需要包装一个对象来存储这些属性。

 public class Model {
   public String spokeCode;
   public String DistributerCode;
   .......
 }

 ArrayList<Model> list = new ArrayList<Model>();
 list.add(model);
 list.add(model);//
 // and so on
 //do search
 DistributerName=rolespinner.getSelectedItem().toString();
 Model searchModel;
 for (Model m : list) {
    if (m.distributerName.equals(DistributerName)) {
         searchModel = m;
         break;
    }
 }

#1


Declare :

 HashMap<String, String> spinnerMap = new HashMap<String, String>();

for (int i = 0; i <= _jarray1.length(); i++) {

                    JSONObject _obj = _jarray1.getJSONObject(i);
                    SpokeCode = _obj.getString("SpokeCode").toString();
                    DistributerCode = _obj.getString("DistributerCode")
                            .toString();
                    DistributerName = _obj.getString("DistributerName")
                            .toString();

                    spinnerMap.put(DistributerName, DistributerCode);
                    stringArray.add(DistributerName);

                }

and spinner item-select Method update this :

和spinner item-select方法更新:

rolespinner.setOnItemSelectedListener(new OnItemSelectedListener() {

rolespinner.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub

        String to=  spinnerMap.get(rolespinner.getSelectedItem());
        Toast.makeText(MainActivity.this,
            to, 10000);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

enjoy!!!

#2


I think you need to wrap a object for storing these property.

我认为你需要包装一个对象来存储这些属性。

 public class Model {
   public String spokeCode;
   public String DistributerCode;
   .......
 }

 ArrayList<Model> list = new ArrayList<Model>();
 list.add(model);
 list.add(model);//
 // and so on
 //do search
 DistributerName=rolespinner.getSelectedItem().toString();
 Model searchModel;
 for (Model m : list) {
    if (m.distributerName.equals(DistributerName)) {
         searchModel = m;
         break;
    }
 }