android spinner项目onclick文本更改

时间:2014-01-28 13:02:35

标签: android

我是android域名的新手。 ,请帮帮我。

我有两个字符串数组,在适配器的帮助下从一个数组我得到微调器下拉视图。事情就是当我点击下拉列表时我必须填充第二个数组中相同位置的值。我粘贴我的代码。请帮帮我。

代码:

class programList extends AsyncTask<String, Long, String> {

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        String url = "http://mydomain.com/app/getdata.php?type=program_list&id="
                + surveyId;
        String output = null;

        JSONParserList Jparser = new JSONParserList();

        output = Jparser.getJSONFromUrl(url);

        return output;
    }

    @Override
    protected void onPostExecute(String output) {
        // TODO Auto-generated method stub

        Log.e("JSONRESPONSE", output);
        try {

            JSONObject jObject = new JSONObject(output);

            JSONArray OUTPUT = jObject.getJSONArray("OUTPUT");
            String[] allStates = new String[OUTPUT.length()+1];
            String[] allDistrics = new String[OUTPUT.length()];
            allStates[0] = "Select Program";


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

                JSONObject eachProgram = OUTPUT.getJSONObject(i);

                String adb = eachProgram.getString("adp");

                String district = eachProgram.getString("district");

                String location = eachProgram.getString("location");

                String p_id = eachProgram.getString("p_id");

                ProgramList programlist = new ProgramList();

                programlist.setAdp(adb);
                programlist.setDistrict(district);
                programlist.setLocation(location);
                programlist.setP_id(p_id);
                // programlist.setAdpList(adb);

                Toast.makeText(getApplicationContext(),
                        "id: " + programlist.getP_id(), Toast.LENGTH_LONG).show();

                programList.add(programlist);
                allStates[i+1] = adb;
                allDistrics[i] = district;
            }
            // programDropDown();
            Spinner sprogram = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<String> adapterstate = new ArrayAdapter<String>(
                    startSurvey.this, android.R.layout.simple_spinner_item,
                    allStates);
            sprogram.setAdapter(adapterstate);

            sprogram.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> adapter, View v,
                        int position, long id) {
                    // TODO Auto-generated method stub

                     String program = adapter.getItemAtPosition(position).toString();
                    // long id1 = adapter.getId();
                     **String d = allDistrics[position-1];**


                        // Showing selected spinner item
                        Toast.makeText(getApplicationContext(),
                                "Program position: " + position, Toast.LENGTH_LONG).show();


                }

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

                }


                });

            // adapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

我的错误是在该明星位置上给出的

1 个答案:

答案 0 :(得分:0)

所有地区都不在该范围内,当您发布此类问题时,您还需要包含实际的日志cat错误

修改

实际上可用的变量可能是你的问题,但我相信的位置就像数组一样零索引,因此也可能是一个问题:position-1 can = -1并且不在边界

要修复区域变量的范围,您可以使用最终关键字final String[] districts ...