【问题标题】:Not able to get correct value using position in spinner无法使用微调器中的位置获得正确的值
【发布时间】:2017-03-11 07:24:23
【问题描述】:

我创建了 2 个微调器,分别是城市和位置。我已经在本地数据库中保存了城市和位置数据,并从本地数据库中获取城市并显示它们。

现在,我的问题是我在数组列表的第 0 个位置添加了“选择城市”位置。当我选择任何城市并运行命令以根据城市获取位置时,我会根据位置得到错误的位置。

我无法获得正确的位置。

// 代码

dumy = new Dumy(this);
cityAreaModelsList = dumy.getOnlyCities();
cityList = new ArrayList<String>();
cityList.add(0, "Select City");

for (int i = 0; i < cityAreaModelsList.size(); i++) {
    cityList.add(cityAreaModelsList.get(i).getCityName());
}

// 微调代码

select_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            Log.e(TAG, "Select City " );
        } else {
            cityId = (String)  cityAreaModelsList.get(position).getCityId();
            Log.e(TAG, "onItemSelected: "+cityId );
        }
    }
}

// 说我选择了新德里,它的城市 id 是 1,但它给了我 2。为什么?请帮忙

【问题讨论】:

    标签: android android-sqlite android-spinner


    【解决方案1】:

    因为适配器的位置从 1 开始(仅数据部分)

    并且 cityAreaModelsList 正在从 0 访问;

    试试下面的代码:

    cityId = (String) cityAreaModelsList.get(position-1).getCityId();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2019-02-25
      相关资源
      最近更新 更多