gpt4 book ai didi

Android adapter.getItem(position).getItemId() 不工作(方法 getId() undefined object 类型)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:22 26 4
gpt4 key购买 nike

我有一个 ListView ,我正在使用自定义适配器将数据填充到 ListView 表单数据库中。它工作正常,但是当我单击列表项时,我想获取被单击以将其传递给下一个 Activity 以执行某些操作的项目的 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;
}

}

我正在从以下代码中获取游标对象(签证)中的数据:

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;
}

}

最佳答案

您必须转换 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<Countries>() .然后,您上面的原始代码行应该按原样工作。

关于Android adapter.getItem(position).getItemId() 不工作(方法 getId() undefined object 类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15781099/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com