Search code examples
androidonclicklistenerlayout-inflater

How to handle click listener of inflated views in android


I have a problem with click listener of inflated view. I want to put different extras to intent for every clickable. How can i achieve this? Here is my xml file that i inflate

<RelativeLayout
        android:id="@+id/cat_item"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/desserts"
        android:clickable="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/header" >

            <TextView
                android:id="@+id/cat_item_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:text=""
                android:textColor="@color/white"
                android:textSize="30sp" />
        </LinearLayout>

    </RelativeLayout>

`And here is my java class

private void setDetails() {
    final int size = Parser.getSizeOfCategoryList();

    for (int i = 0; i < size; i++) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item = inflater.inflate(R.layout.category_item, null);

        itemTitle = (TextView) item.findViewById(R.id.cat_item_title);
        itemTitle.setText(Parser.getCategoryName(i));

        btnCategory = (RelativeLayout) item.findViewById(R.id.cat_item);

        btnCategory.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,
                        SubCategoryActivity.class);

                intent.putExtra("categoryName", Parser.getCategoryName(i));

                startActivity(intent);
                overridePendingTransition(R.anim.activity_out_from_left,
                        R.anim.activity_in_from_right);
            }
        });

        switch (i % 3) {
        case 0:
            columnOne.addView(item);
            break;

        case 1:
            columnTwo.addView(item);
            break;
        case 2:
            columnThree.addView(item);
            break;

        default:
            break;
        }

    }
}

Actually i want to put category names that i get from Arraylist to intent extra. I know that "i" must be final so this gives error but what i try to make something like this. How can i achieve this?

Any help will be appreciated


Solution

  • You can copy i to a final variable and use that.

    final int finalI = i;
    @Override
    public void onClick(View v) {
         intent.putExtra("categoryName", Parser.getCategoryName(finalI));
    }