java - 如何在 ListView 中显示多个图像?

标签 java android json listview

我正在尝试在 ListView 项中显示多个图像。所有数据都完美设置,但是当我在 ListView 项目内显示多个图像时,它在创建时完美显示,但滚动页面后,它在 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

最佳答案

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

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

并在调用此 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);
}

关于java - 如何在 ListView 中显示多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49974715/

相关文章:

java - 如何在 Netbeans IDE 中制作静态类

android - 如何打开存储在SD卡上的PSD文件?

android - Android 上的 OpenGL 线段光栅化

PHP/MySQL - 将数组数据存储为 JSON,不好的做法?

java - 是否有针对 MtGox/PubNub JSON API 的现成 Java 类?

java - 在 Java 中的多态数组对象上运行方法时出现问题

java - 如何调用 "AndroidLauncher"类中 LibGDX 中的方法?

java - Realm Android - 多个库的多 Realm 模块

java - 进度对话框出错

java - 如何轻松更新Json文件的单个属性