在Realmbaseadapter

时间:2016-07-27 08:08:39

标签: android listview realm

根据我的应用程序要求,我在列表视图中显示了一行文本框,图像查看和图像捕获按钮。我正在捕获图像并立即将该图像保存在该领域行中。然后,当调用adaoter getview方法时,适配器从相关领域对象加载该图像。

我的问题是当每个列表的行数据为空,有超过4行并捕获任何图像时;捕获的图像与其意图的位置一起被加载到几个位置。 例如,如果我为位置0拍摄图像,则在位置5中滚动后显示,然后再次重新加载到位置6.我注意到,从位置0滚动到位置6后,适配器位置再次返回0。这是在其他位置滚动期间图像重新加载的原因。当我点击任何按钮进行图片/文本输入时,这个重新加载的行再次返回到正确的位置。

我在listview中经历了与问题imageview改组有关的几乎所有可能建议的SO问题,但它们都不适合我。你能建议任何解决这个问题的方法。 我的适配器代码如下 -

 public class ProgressImageAdapter extends RealmBaseAdapter<ImageCommentRealm> implements ListAdapter {
    private Context mContext;
    String categoryName;
    ProgressListener progressListener;
    PopAlertDialog alertDialog;
    boolean fromHistory;

    public ProgressImageAdapter(Context context, OrderedRealmCollection<ImageCommentRealm> data, ProgressListener progressListener, boolean fromHistory) {
        super(context, data);
        this.mContext = context;
        this.progressListener = progressListener;
        FragmentActivity activity = (FragmentActivity) (context);
        alertDialog = PopAlertDialog.createAlert(activity.getSupportFragmentManager());
        this.fromHistory = fromHistory;
    }

    @Override
    public void updateData(OrderedRealmCollection<ImageCommentRealm> data) {
        super.updateData(data);
    }

    public void resetListener() {
        this.progressListener = null;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.image_comment_list_item, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.tv_title);
            holder.editText = (TextView) convertView.findViewById(R.id.et_comment);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_camera_holder);
            holder.imageButton = (ImageButton) convertView.findViewById(R.id.imgbtn_camera);
            holder.btnDone = (Button) convertView.findViewById(R.id.btn_confirm);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ImageCommentRealm imageCommentRealm = getItem(position);
        holder.textView.setText(String.format(Locale.getDefault(), "%s %d %2s %2s", mContext.getResources().getString(R.string.photo), position + 1, mContext.getString(R.string.of), categoryName));
        if (Validator.blankCheck(imageCommentRealm.getFile_path()))
            loadImage(holder.imageView, imageCommentRealm.getFile_path());
        if (Validator.blankCheck(imageCommentRealm.getDesc()))
            holder.editText.setText(imageCommentRealm.getDesc());
        if (!fromHistory) {
            holder.imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((MainActivity) mContext).pickFromCamera();
                    ((MainActivity) mContext).setPicker(new IPhotoPicker() {
                        @Override
                        public void setImageUri(String filePath) {
                            //loadImage(holder,filePath);
                            saveData(null, filePath, imageCommentRealm.getPk());

                        }
                    });
                }
            });
            holder.editText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String title = String.format(Locale.getDefault(), "%s %d %2s %2s", mContext.getResources().getString(R.string.description), position + 1, mContext.getString(R.string.of), categoryName);
                    alertDialog.showSingleInput(title, imageCommentRealm.getDesc(), imageCommentRealm.getPk(), new SingleInput.Listener() {
                        @Override
                        public void onClickGreen(String message, long id) {
                            if (Validator.blankCheck(message)) saveData(message, null, id);
                        }
                    });
                }
            });
        }
        return convertView;
    }

    protected void loadImage(ImageView imageView, String imgPath) {
        if (imgPath.startsWith("http")) {
            ImageUtility.loadProduct(mContext, imgPath, imageView, R.drawable.home);
        } else {
            try {
                File image_file = new File(imgPath);
                if (image_file.exists()) {
                    Bitmap image_profile = BitmapFactory.decodeFile(image_file.getAbsolutePath());
                    imageView.setImageBitmap(image_profile);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private void saveData(final String comment, final String filepath, final long pk) {
        final Realm realm = Realm.getInstance(RealmUtility.getRealmConfig(mContext));
        //ImageCommentRealm imageRealm = getItem(position);
        //final long pk = imageRealm.getPk();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                ImageCommentRealm imageCommentRealm = realm.where(ImageCommentRealm.class).equalTo("pk", pk).findFirst();
                if (imageCommentRealm != null && imageCommentRealm.isLoaded()) {
                    if (Validator.blankCheck(comment)) {
                        imageCommentRealm.setDesc(comment);
                    }
                    if (Validator.blankCheck(filepath)) {
                        imageCommentRealm.setFile_path(filepath);
                    }
                }

            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {

                //notifyDataSetChanged();
                realm.close();
                try {
                    if (adapterData != null && adapterData.size() > 0) {
                        long count = adapterData.where().isNotNull("file_path").isNotNull("desc").count();
                        if (count == adapterData.size()) {
                            if (progressListener != null) progressListener.hasRequiredImage();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                error.printStackTrace();
                realm.close();
            }
        });
    }

    static class ViewHolder {
        TextView textView;
        TextView editText;
        ImageView imageView;
        ImageButton imageButton;
        Button btnDone;
    }
}

这是在第一个位置拍摄照片然后滚动到底部后的屏幕截图 enter image description here

仅供参考:测试装置4&#34;三星J1 Nxt

1 个答案:

答案 0 :(得分:1)

呃,这与Realm无关,如果图像路径为空,你应该清除图像。

    if (Validator.blankCheck(imageCommentRealm.getFile_path())) {
        loadImage(holder.imageView, imageCommentRealm.getFile_path());
    } else {
        holder.imageView.setImageBitmap(null);
    }
    if (Validator.blankCheck(imageCommentRealm.getDesc()))
        holder.editText.setText(imageCommentRealm.getDesc());
    } else {
        holder.editText.setText("");
    }