Johan
Johan

Reputation: 8276

Find view in dynamically added layouts

So I have a layout with an Add button, which when clicked adds a custom layout to the LinearLayout.

In this custom layout, I have an image, that when clicked should increase a counter (displayed in a textView next to the image).

The problem I am currently having is, that when I have more than one custom layout added, and I click the image it always increases the count of the textView right on top and not the textView next to the image clicked.

How would I find the correct textView to change?

I am fairly new to the android dev world, so if I am doing anything wrong please add that too.

Regards

Upvotes: 0

Views: 55

Answers (1)

Lingviston
Lingviston

Reputation: 5671

Set OnClickListener to each ImageView when you're inflating your custom layout. In this listener refer to a final TextView variable which repesents a TextView exactly from the currently created layout. Something like this:

    private void addView() {
        View customView = getLayoutInflater.inflate(resId, null);
        final TextView textView = (TextView) customView.findViewById(R.id.textview);
        customView.findViewById(R.id.imageview).setOnClickListener(new OnClickLitener(View v) {
            textView.setText("value");
        });
        container.addView(customView);
    }

Upvotes: 1

Related Questions