简体   繁体   中英

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

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM