如何在Listview中显示多个图像?

时间:2018-04-23 06:31:40

标签: java android json listview

我正在尝试在列表视图项目中显示多个图像。所有数据完美设置,但当我在列表视图项目中显示多个图像时,它在创建时显示完美,但在滚动页面后,它显示列表视图项目内的双重图像。我是以编程方式创建ImageView并添加线性布局。另外如何获得ImageView的位置。我附上下面的图片。如何修复此错误

这是我的适配器代码。

    @NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
    final ApproveReimbBin item = getItem(position);
    FoldingCell cell = (FoldingCell) convertView;
    ViewHolder viewHolder;

    String date = item.getStr_startDate();
    String[] splitDate = date.split("To");

    String profileName = item.getStr_name();
    String profileEmpId = item.getStr_empId();
    String profileType = item.getStr_type();
    String profileAmount = item.getStr_amount();
    String[] childDocument = item.getStr_documents();

    if (cell == null) {
        viewHolder = new ViewHolder();
        LayoutInflater vi = LayoutInflater.from(getContext());
        cell = (FoldingCell) vi.inflate(R.layout.adapter_approvereimburs, parent, false);

        // binding view parts to view holder
        viewHolder.imag_listProfileImage = cell.findViewById(R.id.list_image);
        viewHolder.txt_listName = cell.findViewById(R.id.list_profileName);
        viewHolder.txt_listEmpId = cell.findViewById(R.id.list_profileEmpId);
        viewHolder.txt_listType = cell.findViewById(R.id.list_profileType);

        viewHolder.image_childProfileImage = cell.findViewById(R.id.child_profile_img);
        viewHolder.txt_name = cell.findViewById(R.id.txt_profile_name);
        viewHolder.txt_empId = cell.findViewById(R.id.txt_profile_id);
        viewHolder.txt_type = cell.findViewById(R.id.txt_profile_type);

        viewHolder.txt_frmDate = cell.findViewById(R.id.txt_from_date);
        viewHolder.txt_toDate = cell.findViewById(R.id.txt_to_date);
        viewHolder.txt_amount = cell.findViewById(R.id.txt_amount);
        viewHolder.btn_reject = cell.findViewById(R.id.btn_reject);
        viewHolder.btn_approve = cell.findViewById(R.id.btn_approve);
        viewHolder.linearLayout = cell.findViewById(R.id.linearLayout_Image);

        cell.setTag(viewHolder);
    } else {
        if (unfoldedIndexes.contains(position)) {
            cell.unfold(true);
            Log.e("suraj", "unfold call");
        } else {
            cell.fold(true);
            Log.e("suraj", "fold call");
        }
        viewHolder = (ViewHolder) cell.getTag();
    }

    if (null == item)
        return cell;


    String ImageUrl = ServerUrls.Web.IMAGE_URL + profileEmpId.trim() + ".jpg";

    ImageView image;
    if (childDocument.length > 0) {
        for (int i = 0; i < childDocument.length; i++) {

            String doc = childDocument[i].replace("~", "");
            doc = doc.replace(",", "");

            image = new ImageView(mContext);
            image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80, 60));
            image.setMaxHeight(50);
            image.setMaxWidth(50);

            Picasso.with(mContext)
                    .load(imageDocument.trim() + doc)
                    .error(R.drawable.doc_img)
                    .into(image);

            viewHolder.linearLayout.addView(image);
            int lent = childDocument.length;
            Log.e("surajj", "docu " + imageDocument.trim() + doc + " position " + i + " lenth " + lent);
        }
    } else {
        viewHolder.linearLayout.setVisibility(View.GONE);
    }

    Picasso.with(mContext)
            .load(ImageUrl.trim())
            .error(R.drawable.ic_user)
            .into(viewHolder.imag_listProfileImage);

    Picasso.with(mContext)
            .load(ImageUrl.trim())
            .error(R.drawable.ic_user)
            .into(viewHolder.image_childProfileImage);


    if (profileName != null || !profileName.equals(" ")) {
        viewHolder.txt_listName.setText(profileName);
        viewHolder.txt_name.setText(profileName);
    }
    if (profileEmpId != null || !profileEmpId.equals(" ")) {
        viewHolder.txt_listEmpId.setText(profileEmpId);
        viewHolder.txt_empId.setText(profileEmpId);
    }
    if (profileType != null || !profileType.equals(" ")) {
        viewHolder.txt_listType.setText(profileType);
        viewHolder.txt_type.setText(profileType);
    }
    if (profileAmount != null || !profileAmount.equals(" ")) {
        viewHolder.txt_amount.setText(profileAmount);
    }

    if (splitDate != null || !splitDate.equals(" ")) {

        if (splitDate.length == 2) {
            Log.d("suraj1", "fromDate " + splitDate[0] + " toDate " + splitDate[1]);

            viewHolder.txt_frmDate.setText(splitDate[0]);
            viewHolder.txt_toDate.setText(splitDate[1]);
        } else {
            Log.d("suraj1", "onlyfromDate " + splitDate[0]);
            viewHolder.txt_frmDate.setText(splitDate[0]);
        }
    }

    viewHolder.btn_approve.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            requestId = item.getStr_requestId();
            reimbursmentId = item.getStr_reimbursementid();
            action = "Approved";
            Methods.showProgressDialog(mContext);
            unfoldedIndexes.clear();
            new asyncSendRequest().execute();
        }
    });

    viewHolder.btn_reject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            requestId = item.getStr_requestId();
            reimbursmentId = item.getStr_reimbursementid();
            action = "Rejected";
            Methods.showProgressDialog(mContext);
            unfoldedIndexes.clear();
            new asyncSendRequest().execute();
        }
    });

    return cell;
}

这是我的Json

[  {    "documents": [
  "~\\Upload\\10 Nov 2017\\KB94L636459349491385669.jpg,"
],
"username": "Pranav Khanvilkar",
"empcode": "313395",
"type": "TRAIN / BUS PASS",
"startEndDate": "03-Nov-2017 To 02-Dec-2017",
"requestid": "1916",
"amount": "265",
"reimbursementid": "3",
"applyDate": "11/10/2017 6:22:29 PM"  },  {
"documents": [
  "~\\Upload\\07 Apr 2018\\XHKZU636586950672544266.jpg,",
  "~\\Upload\\07 Apr 2018\\2V44D636586950672674273.jpg,"
],
"username": "Anil Pawar",
"empcode": "313325",
"type": "SALES VISIT",
"startEndDate": "05-Apr-2018 To 06-Apr-2018",
"requestid": "8448",
"amount": "400",
"reimbursementid": "4",
"applyDate": "4/7/2018 10:51:04 AM"  }]

这是我遇到的问题。 在滚动页面之前。 enter image description here

滚动页面后。 enter image description here

2 个答案:

答案 0 :(得分:2)

在for循环之前使用以下代码。

if (viewHolder.linearLayout..getChildCount()>0)
viewHolder.linearLayout.removeAllViews();

并在调用this for for循环

之后
if (childDocument.length > 0) {
    for (int i = 0; i < childDocument.length; i++) {

        String doc = childDocument[i].replace("~", "");
        doc = doc.replace(",", "");

        image = new ImageView(mContext);
        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80, 60));
        image.setMaxHeight(50);
        image.setMaxWidth(50);

        Picasso.with(mContext)
                .load(imageDocument.trim() + doc)
                .error(R.drawable.doc_img)
                .into(image);

        viewHolder.linearLayout.addView(image);
        int lent = childDocument.length;
        Log.e("surajj", "docu " + imageDocument.trim() + doc + " position " + i + " lenth " + lent);
    }
} else {
    viewHolder.linearLayout.setVisibility(View.GONE);
}

答案 1 :(得分:0)

请致电

viewHolder.linearLayout.removeAllViews();
在你的for循环之前

    for (int i = 0; i < childDocument.length; i++) {
         ...
    }