Android adapter.getItem(position).getItemId() not working (The method getId() is undefined for the type Object)

29,967

Solution 1

You must cast the getItem(position) return value to a Countries object.

Change this:

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

To this:

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

Update:

Actually, after reading @Tushar's comment, I think you should make names a VisaAdapter instead of an ArrayList<Countries>(). Then, your original line of code, above, should work as-is.

Solution 2

adapter.getItem(position)

function Returns an Object so type cast it to Country like

(Countries)adapter.getItem(position)
Share:
29,967
suresh cheemalamudi
Author by

suresh cheemalamudi

Android Developer

Updated on April 03, 2020

Comments

  • suresh cheemalamudi
    suresh cheemalamudi about 4 years

    I have a listview and i am populating the data to the list view form DB using custom adapter. Its working fine, but when i click on the list item, i want to get the ID of the item which is clicked to pass it on to the next activity to do some stuff. But when i click on list item, i am not able to get the ID. Any idea why is this happening?

    my list listener:

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

    But i have defined my countries.java (POJO) for type specification

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

    i am fetching data out of cursor obj (visas) from the below code:

    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());
    

    here is my adapter code:

    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;
        }
    
    }
    
    • Tushar
      Tushar about 11 years
      You don't need to create values inside VisaAdapter (ArrayAdapter already stores them...no need for duplication) and you don't need to override getItem as what you have is the default implementation anyways.
    • Tushar
      Tushar about 11 years
      Can you show us where you declare the adapter variable?
    • suresh cheemalamudi
      suresh cheemalamudi about 11 years
      @Tushar thanks for the answers. David's answer works. But if i remove values from VisaAdapter, how can i get reference to inflate the data onto listview, for example : values.get(position).getCountryName());
    • Tushar
      Tushar about 11 years
      You'd use getItem(position).getCountryName().
  • Tushar
    Tushar about 11 years
    This may fix the problem, but it shouldn't be the case. Look up the signature of getItem()
  • Tushar
    Tushar about 11 years
    Does it? You should look up the signature of getItem. ArrayAdapter<T>.getItem() returns T.
  • Triode
    Triode about 11 years
    adapter I am also concerned about the type of adapter variable ?
  • David Manpearl
    David Manpearl about 11 years
    You're right @Tushar. I've edited my answer based on your input. If you add an answer of your own, suresh would do best to accept yours as correct.
  • Kaustubh Bhagwat
    Kaustubh Bhagwat over 6 years
    ((ConvoResponseModel.DataBean.ConversationBean) adapter.getItemId(0)).getTime(); i am trying it this way but getting inconvertible cannot cast long to ConversationBean how do i solve this?
  • Ashwin H
    Ashwin H almost 6 years
    its working, in my case, StateBean stateBean = (StateBean) stateAdapter.getItem(position); mEditTextState.setText(stateBean.getName()); mStateId = stateBean.getId();