0

I'm not exactly sure what's going wrong here, but no matter where I try to force my imageview to be visible, it won't show up unless I call onCreate(null), which is a problem because it basically reinitializes my activity and all the live data is lost.

Basically this imageView is changed everytime someone drops it on the red box in my app, but instead of changing it just dissapears. I have verfied that nowhere in my code is View.Gone ever called and the image should never disappear in the first place.

  public void setDragProps(ImageView image, ImageView green, ImageView red){
    image.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                default:
                    return true;
            }
        }
    });

    green.setOnDragListener(new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DROP:
                    Log.d(msg, "Drag ended");
                    if (dropEventNotHandled(event)) {
                        v.setVisibility(View.VISIBLE);
                    }
                    CharSequence options[] = new CharSequence[]{currentItem.getMenu().get(itemCounter-1).getName(), currentItem.getAddress(), currentItem.getPhone(),currentItem.getRating()};
                    AlertDialog.Builder builder = new AlertDialog.Builder(TouchActivity.this);
                    builder.setTitle(currentItem.getName());
                    builder.setItems(options, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch(which){
                                case 1: break;
                                //case 2: Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                                //Uri.parse("google.navigation:q=an+address+city"));
                                // break;
                                case 3: break;
                                case 4: break;
                            }
                        }
                    });
                    builder.show();
                    break;
                default:
                    return true;
            }
            return false;
        }
    });


    red.setOnDragListener(new View.OnDragListener() {
        int i = 0;
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DROP:
                    showNextImage();
                    v.setVisibility(View.VISIBLE);
                    onCreate(null);
                    Log.d(msg, "Drag ended");
                    if (dropEventNotHandled(event)) {
                        v.setVisibility(View.VISIBLE);
                    }
                    break;
                default:
                    return true;
            }
            return false;
        }
    });


    image.setOnTouchListener(new View.OnTouchListener() {
        @Override

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.i("Touch Listener","Action down triggered");
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(img);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        v.startDragAndDrop(data, shadowBuilder, img, 100);
                    } else //noinspection deprecation,deprecation,deprecation
                        v.startDrag(data, shadowBuilder, img, 0);
                    default:
                        Log.i("Touch Listener","default triggered");
                        v.setVisibility(View.VISIBLE);
            }



                return true;
            }


    });
}

Show Next Image:

    public void showNextImage() {
    DownloadImageTask imageLoader = new DownloadImageTask(img);
    if (!currentItem.getMenu().isEmpty() && currentItem.getMenu().size() > itemCounter) {

        Log.i("Touch Activity ", "Loading New Image ---> " + currentItem.getMenu().get(itemCounter).getImagePath()+ " From " + currentItem.getName());
        imageLoader.execute(currentItem.getMenu().get(itemCounter).getImagePath());
        img.setVisibility(View.VISIBLE);
        itemCounter++;

    } else {
        Log.i("Touch Activity ", "Can't find any more images to download from " + currentItem.getName() + ". Loading next restaurant... ");
        getNextRestaurant();
    }
}

DownloadImageTask:

    // This class takes a URL and downloads the image at the url.
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView tempImage;
    public boolean doneLoading = false;


    public DownloadImageTask(ImageView bmImage) {
        this.tempImage = bmImage;
        tempImage.setScaleType(ImageView.ScaleType.FIT_XY);

    }

    protected Bitmap doInBackground(String... urls) {
        String inputURL = urls[0];
        Bitmap tempBmap = null;

        try {
            InputStream in = new java.net.URL(inputURL).openStream();
            tempBmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return tempBmap;
    }

    protected void onPostExecute(Bitmap result) {
        doneLoading = true;
        tempImage.setImageBitmap(result);
        tempImage.setVisibility(View.VISIBLE);
    }
}
Remixt
  • 597
  • 6
  • 28
  • Can you post your `showNextImage()` method? – Nic Robertson Dec 05 '16 at 01:35
  • Just added it. Thanks. – Remixt Dec 05 '16 at 01:40
  • You should really consider using something like http://square.github.io/picasso/ or https://github.com/bumptech/glide this will make your life a lot easier. It will perform all of the downloading/caching/loading into your image views with just 1 line of code. No reason to re-invent the wheel. – Nic Robertson Dec 05 '16 at 01:42
  • I tried using both of those, a little too complicated to implement for my current amount of android studio knowledge. This was much easier. – Remixt Dec 05 '16 at 01:43
  • 1
    No, its not much easier. If adding picasso or glide to your project is too difficult then you might want to consult some basic android guides/tutorials. You will run into more of the same problems if you never learn this stuff. You literally need to add 1 line to your app build.gradle file and you are ready to use it. Much cleaner than adding copy-pasted stack overflow code that doesn't seem to be working for you – Nic Robertson Dec 05 '16 at 01:46
  • It works just fine, it's the drag and drop stuff that is being problematic. I followed a tutorial on the picaso github and it required quite a bit more than a single line of code. I might revisit it if you insist, but at the moment I'm trying to solve this particular issue. Thanks for the advise though, I appreciate it. – Remixt Dec 05 '16 at 01:49
  • 2
    `Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);` is the only line you need to load an image into an imageview. You also need to learn to use the debugger if you aren't already using it. Put in some breakpoints and figure out what is causing your issues. You can also debug ui elements by using the uiautomatorviewer that ships with android. – Nic Robertson Dec 05 '16 at 01:54
  • Well that's handy! thanks for the syntax. With Picasso it's still disappearing, but I appreciate the help with that in any case. XD – Remixt Dec 05 '16 at 02:13

0 Answers0