-1

I want to delete view when clicking on a delete icon but it only removes last view whenever I click on delete icon.

 for (int i = 0; i < response.body().getData().getQualifications().size(); i++) {
        View item = qualificationInflater.inflate(R.layout.student_education_view, null);
        qualificationParentPanel.addView(item);
        TextView college = (TextView) item.findViewById(R.id.college);
        TextView degree = (TextView) item.findViewById(R.id.degree);
        TextView percentage = (TextView) item.findViewById(R.id.percentage);
        TextView start_date = (TextView) item.findViewById(R.id.start_date);
        TextView end_date = (TextView) item.findViewById(R.id.end_date);
        ImageView educationDeleteIcon = (ImageView) item.findViewById(R.id.educationDeleteIcon);
        college.setText(response.body().getData().getQualifications().get(i).getCollege());
        degree.setText(response.body().getData().getQualifications().get(i).getDegree());
        percentage.setText(response.body().getData().getQualifications().get(i).getPercentage());
        start_date.setText(response.body().getData().getQualifications().get(i).getSessionFrom());
        end_date.setText(response.body().getData().getQualifications().get(i).getSessionTo());

        final int finalI = i;
        educationDeleteIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("deleteIconPos","------>" +response.body().getData().getQualifications().get(finalI).getId());
               qualificationParentPanel.removeViewAt(finalI);
            }
        });
Prabhat Rai
  • 1,532
  • 2
  • 10
  • 12

2 Answers2

1

Attach i with every ImageView using setTag

educationDeleteIcon.setTag(i);

then later

 qualificationParentPanel.removeViewAt((int)v.getTag());
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

consider finalizing freshly inflated view

final View item = ...

and then inside OnClickListener you may use

qualificationParentPanel.removeView(item);

also you should use only local declaration for educationDeleteIcon

ImageView educationDeleteIcon = (ImageView) item.findViewById(R.id.educationDeleteIcon);

instead of

educationDeleteIcon = (ImageView) item.findViewById(R.id.educationDeleteIcon);
snachmsm
  • 17,866
  • 3
  • 32
  • 74