emen
emen

Reputation: 6318

MergeAdapter with OnClickItemListener

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

Upvotes: 2

Views: 1562

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

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.

Upvotes: 4

Related Questions