2

I'm using MergeAdapter by CommonsWare to merge two ListViews.

Note that, both listviews have its own Model.

Everything goes well except when I want to click an item from the merged Listview, I can't seem to get the correct data represented their corresponding model. I've seen this answer, though it doesn't have any full solution to what I am looking for.

This is how I implement the MergeAdapter:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // merge adapter
        mAdapter  = new MergeAdapter();

        // user list
        arrUserVanities = new ArrayList<UserVanitiesModel>();
        uva = new UserVanitiesAdapter(arrUserVanities, getActivity());
        mAdapter.addAdapter(uva);

        // user category
        arrUserCategory = new ArrayList<UserCategoriesModel>();
        uca = new UserCategoryAdapter(arrUserCategory, getActivity());
        mAdapter.addAdapter(uca);


        setListAdapter(mAdapter);
   }

In onItemClickListener:

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);


        UserVanitiesModel vanityModel = (UserVanitiesModel) l.getAdapter().getItem(position);
        String vanity_id = vanityModel.getVanity_id();
        String vanity_name = vanityModel.getVanity_name();


        UserCategoriesModel categoryModel = (UserCategoriesModel) l.getAdapter().getItem(position);
        String category_id = categoryModel.getCategory_id();
        String category_name = categoryModel.getCategory_name();

        Log.d(TAG, "Item clicked: "+category_name);
        Log.d(TAG, "Item clicked: "+vanity_name);

        //  mListener.onUserCategoriesSelected(category_id, category_name);
    }

I'm stuck at this. How do I get the corresponding Object in the merged listview and obtain any data from it? I'm passing those data to an interface listener.


UPDATE

I got two Listviews to be merged, namely, User List (backed by UserVanitiesAdapter) and User Category (backed by UserCategoriesAdapter)

This is the error I got when an item is clicked:

java.lang.ClassCastException: com.example.model.usershop.UserCategoriesModel cannot be cast to com.example.model.usershop.UserVanitiesModel

or the other way around if the second listview is clicked.

java.lang.ClassCastException: com.example.model.usershop.UserVanitiesModel cannot be cast to com.example.model.usershop.UserCategoriesModel
Community
  • 1
  • 1
emen
  • 6,050
  • 11
  • 57
  • 94

1 Answers1

4

You are calling l.getAdapter().getItem(position) twice and casting it to two different classes. Unless those classes are related by inheritance, by definition, one will crash with a ClassCastException.

Only call l.getAdapter().getItem(position) once. You will need to determine which class to cast to either based on position or by using the instanceof operator.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm not quite familiar with `instanceof` operator. Do I have to override library's getItem in order to know which adapter does the clicked item came from? – emen Feb 26 '14 at 07:37
  • 2
    No. instanceof is part of standard Java. Or, have both models implement some common interface, cast to that interface, and call some method on the interface to determine what to do with the object. – CommonsWare Feb 26 '14 at 10:14