【问题标题】:Android adapter.getItem(position).getItemId() not working (The method getId() is undefined for the type Object)Android adapter.getItem(position).getItemId() 不工作(方法 getId() 未定义对象类型)
【发布时间】:2013-04-03 07:16:35
【问题描述】:

我有一个列表视图,我正在使用自定义适配器将数据填充到列表视图表单数据库中。它工作正常,但是当我单击列表项时,我想获取被单击的项目的 ID,以将其传递给下一个活动以做一些事情。但是当我单击列表项时,我无法获取 ID。 知道为什么会这样吗?

我的列表监听器:

    list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> a, View view, int position,
                        long id) {


                    Intent intent = new Intent(MainActivity.this,
                            CountryActivity.class);
                     intent.putExtra("countryId", adapter.getItem(position).getId());//here i am getting error saying 
          //The method getId() is undefined for the type Object.

                    startActivity(intent);

                }
            });

但我已经为类型规范定义了我的 countries.java (POJO)

public class Countries {
    Integer id;
    String countryName = "", countryIcon = "";

    public Countries(Integer id, String countryName, String countryIcon) {

        this.id = id;
        this.countryName = countryName;
        this.countryIcon = countryIcon;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getCountryIcon() {
        return countryIcon;
    }

    public void setCountryIcon(String countryIcon) {
        this.countryIcon = countryIcon;
    }

}

我正在从以下代码中从光标 obj(签证)中获取数据:

names = new ArrayList<Countries>();
        do {

            country = new Countries(Integer.parseInt(visas.getString(0)),
                    visas.getString(1), visas.getString(2));

            // Log.d("VISA", visas.getString(0));

            names.add(country);

        } while (visas.moveToNext());

这是我的适配器代码:

public class VisaAdapter extends ArrayAdapter<Countries> {
    private final Context context;
    private final ArrayList<Countries> values;
    Resources resc = getContext().getResources();

    public VisaAdapter(Context context, ArrayList<Countries> values) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public Countries getItem(int position) {
        return values.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(values.get(position).getCountryName());

        // Change icon based on name
        String icon = values.get(position).getCountryIcon();
        int resIcon = resc.getIdentifier(icon, "drawable", "com.m7.visaapp");

        rowView.setBackgroundResource(R.drawable.list_view_places_row);

        imageView.setImageResource(resIcon);

        return rowView;
    }

}

【问题讨论】:

  • 你不需要在VisaAdapter 中创建valuesArrayAdapter 已经存储了它们......不需要重复)并且你不需要覆盖getItem无论如何,您拥有的是默认实现。
  • 您能告诉我们您在哪里声明了adapter 变量吗?
  • @Tushar 感谢您的回答。大卫的回答有效。但是,如果我从 VisaAdapter 中删除值,我如何获得将数据膨胀到列表视图的参考,例如:values.get(position).getCountryName());
  • 你会使用getItem(position).getCountryName()

标签: android android-listview listviewitem android-adapter


【解决方案1】:

您必须将 getItem(position) 返回值强制转换为 Countries 对象。

改变这个:

    intent.putExtra("countryId", adapter.getItem(position).getId());

到这里:

    Countries countries = (Countries)adapter.getItem(position);
    intent.putExtra("countryId", countries.getId());

更新

实际上,在阅读@Tushar 的评论后,我认为您应该将names 设为VisaAdapter 而不是ArrayList&lt;Countries&gt;()。然后,您上面的原始代码行应该可以按原样工作。

【讨论】:

  • 这可能会解决问题,但情况并非如此。查getItem()的签名
  • 你是对的@Tushar。我已经根据您的输入编辑了我的答案。如果您添加自己的答案,suresh 会尽力接受您的正确答案。
  • 它的工作,在我的例子中,StateBean stateBean = (StateBean) stateAdapter.getItem(position); mEditTextState.setText(stateBean.getName()); mStateId = stateBean.getId();
【解决方案2】:
adapter.getItem(position)

函数返回一个Object,因此将其类型转换为 Country like

(Countries)adapter.getItem(position)

【讨论】:

  • 是吗?您应该查找getItem 的签名。 ArrayAdapter&lt;T&gt;.getItem() 返回T
  • adapter 我也关心适配器变量的类型?
  • ((ConvoResponseModel.DataBean.ConversationBean) adapter.getItemId(0)).getTime();我正在尝试这种方式,但无法转换不能长时间转换为 ConversationBean 我该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
相关资源
最近更新 更多