4

リストビューがあり、カスタム アダプターを使用してリスト ビュー フォーム DB にデータを入力しています。正常に動作していますが、リスト項目をクリックすると、クリックされた項目の 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 (visas) からデータを取得しています。

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

}
4

2 に答える 2

6
adapter.getItem(position)

関数 so 型を返し、ObjectCountry のようにキャストします

(Countries)adapter.getItem(position)
于 2013-04-03T07:24:31.897 に答える