arafat55
arafat55

Reputation: 23

Can not determine which item is clicked

From a list I want to see which item is clicked. If an item is clicked then it can be in different color so that we can separate it. Here I have attached a switch-case bolow. Can you please help me how to determine the clicked item?

private View getSubCategoryListItemView(final SubCategoryItem si, double dwPercentage, final int cat_id) {
    LayoutInflater li = LayoutInflater.from(this);
    View vv = li.inflate(R.layout.sub_cat_list_item, llCatListHolder, false);
    ImageView ivIcon = (ImageView) vv.findViewById(R.id.iv_sub_cat_icon);
    final TextView tvName = (TextView) vv.findViewById(R.id.tv_sub_cat_name);
    ivIcon.setImageResource(AppConstants.ALL_CAT_MARKER_ICONS[cat_id-1]);
    ViewGroup.LayoutParams lpIv = ivIcon.getLayoutParams();
    lpIv.width = (int) (primaryIconWidth * dwPercentage);
    ivIcon.setLayoutParams(lpIv);
    tvName.setText(si.getSubcatHeader());
    tvName.setTextSize((float) (VIEW_WIDTH * .10 * dwPercentage));

    /**
     *
     *This OnClickListener will be called for clicking subcategory items from the top list
     */
    //  tvName.setTextColor(Color.WHITE);
    vv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            ArrayList<SubCategoryItem> subCategoryItems = getSubCategoryList(cat_id);
            for(SubCategoryItem si : subCategoryItems) {
                //    tvName.setTextColor(Color.WHITE);
            }
            tvName.setTextColor(Color.RED);
            /*code for category*/
            /*following code will be different for each category*/
            /*category id 1 means education.
            * category id 2 means health
            * category id 3 means entertainment
            * category id 4 means government
            * category id 5 means legal
            * category id 6 means financial
            * category id 7 means job*/
            // tvName.setTextColor(Color.WHITE);
            switch (currentCategoryID) {
                case AppConstants.EDUCATION:
                    ArrayList<EducationServiceProviderItem> eduItem = constructEducationListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithEducationInfo(si.getSubcatHeader(), cat_id, eduItem);
                    break;
                case AppConstants.HEALTH:
                    //TODO write necessary codes for health
                    ArrayList<HealthServiceProviderItem> healthItem = constructHealthListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithHealthInfo(si.getSubcatHeader(), cat_id, healthItem);
                    break;
                case AppConstants.ENTERTAINMENT:
                    tvName.setTextColor(Color.GREEN);
                    ArrayList<EntertainmentServiceProviderItem> entItem = constructEntertainmentListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithEntertainmentInfo(si.getSubcatHeader(), cat_id, entItem);
                    break;
                //TODO write necessary codes for entertainment
                case AppConstants.GOVERNMENT:
                    //TODO write necessary codes for government
                    break;
                case AppConstants.LEGAL:
                    ArrayList<LegalAidServiceProviderItem>legalItem = constructlegalaidListItemForHeader(cat_id,si.getSubcatHeader());
                    callMapFragmentWithLegalAidInfo(si.getSubcatHeader(),cat_id,legalItem);
                    break;
                case AppConstants.FINANCIAL:
                    ArrayList<FinancialServiceProviderItem> financialItem = constructfinancialListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithFinancialInfo(si.getSubcatHeader(), cat_id, financialItem);
                    break;
                case AppConstants.JOB:
                    ArrayList<JobServiceProviderItem> jobItem = constructjobListItemForHeader(cat_id, si.getSubcatHeader());
                    callMapFragmentWithJobInfo(si.getSubcatHeader(), cat_id, jobItem);
                    break;
                default:
                    break;
            }
            /*code for all*/
            showSubCatListItem.setEnabled(true);
            subCatItemListHeader.setText(si.getSubcatHeader());
            constructSubCategoryItemList(cat_id, si.getSubcatHeader());
        }
    });
    return vv;
}

Upvotes: 2

Views: 51

Answers (2)

Mazharul Islam
Mazharul Islam

Reputation: 70

Add the listener to the items. Listview is preferable.

Upvotes: 1

Joachim
Joachim

Reputation: 2771

Add the listener to the items not the whole view, then the "View v" is the item that was clicked.

Otherwise you can also use a ListView and setOnItemClickListener and that will give you the item in the list which was clicked but that's going to depend on what you're trying to do.

Upvotes: 1

Related Questions