java.lang.String cannot be cast to

849 Views Asked by At

I am trying to get a firebase database value when I click on an autocomplete suggested item. The code below is how I am trying to implement it. However, I seem to be getting a java.lang.String cannot be cast to SearchInformation error on this line:

sInfo = (SearchInformation) adapterView.getAdapter().getItem(i);

I am following the first answer from this example.

Note: I do not know if this is the right way of retrieving information from firebase database using autocomplete, but to find out, I need to solve this first.

Code:

public class Hot extends Fragment {

    private SearchInformation sInfo;
    private DatabaseReference database;


    public Hot() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_hot, container, false);

        database = FirebaseDatabase.getInstance().getReference();

        final ArrayAdapter<String> autoComplete = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1);

        database.child("AutoCompleteOptions").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot suggestionSnapshot : dataSnapshot.getChildren()){

                    sInfo = suggestionSnapshot.getValue(SearchInformation.class);

                    autoComplete.add(sInfo.getEatery_name());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        AutoCompleteTextView ACTV= rootView.findViewById(R.id.actv);
        ACTV.setAdapter(autoComplete);
        ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                sInfo = (SearchInformation) adapterView.getAdapter().getItem(i); // ** ERROR HERE **
                String anID = sInfo.getEatery_id(); 

                Log.i("ID is", anID);

            }
        });
        return rootView;
    }

}

Data model:

public class SearchInformation {

    private String eatery_name;
    private String eatery_id;

    public SearchInformation() {

    }

    public SearchInformation(String eatery_name, String eatery_id) {
        this.eatery_name = eatery_name;
        this.eatery_id = eatery_id;

    }

    public String getEatery_name() {
        return eatery_name;
    }

    public void setEatery_name(String eatery_name) {
        this.eatery_name = eatery_name;
    }

    public String getEatery_id() {
        return eatery_id;
    }

    public void setEatery_id(String eatery_id) {
        this.eatery_id = eatery_id;
    }

}
2

There are 2 best solutions below

0
On

That is because it is a ArrayAdapter<String>, there are Strings in it. Your getItem() therefore also gives you a String.

3
On

You need to change ArrayAdapter<String> to custom adapter ArrayAdapter<SearchInformation>. Then using autoComplete.add(sInfo) you can get whole object of SearchInformation with adapterView.getAdapter().getItem(i)