Android视图使用for循环进行充气

时间:2017-08-03 17:40:27

标签: android view layout-inflater

我想在点击删除图标时删除视图,但只有在我点击删除图标时才删除最后一个视图。

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

2 个答案:

答案 0 :(得分:1)

使用setTag

i ImageView添加educationDeleteIcon.setTag(i);
 qualificationParentPanel.removeViewAt((int)v.getTag());

然后再

Me.button1.Visible = Not IsNull(DLookup("ID", "queryname", "ID=1"))

答案 1 :(得分:0)

考虑最终确定新近膨胀的观点

final View item = ...

然后在OnClickListener内你可以使用

qualificationParentPanel.removeView(item);

你也应该只使用educationDeleteIcon

的本地声明
ImageView educationDeleteIcon = (ImageView) item.findViewById(R.id.educationDeleteIcon);

而不是

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