How do I use touchlistener to load new images on my Java Android app?

  • Context: Java 
  • Thread starter Thread starter whitehorsey
  • Start date Start date
  • Tags Tags
    Android Java
Click For Summary

Discussion Overview

The discussion revolves around implementing a touch listener in a Java Android app to load new images based on user interactions. The specific scenario involves a series of choices where the user selects images representing days of the week, followed by months, and the challenge is ensuring that the new images load only after a selection is made.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes the initial setup of the app with two rows of images and the intended functionality of loading new images upon user selection.
  • Another participant questions the clarity of the initial explanation and seeks confirmation on whether the goal is to load different images after a selection is made.
  • Concerns are raised about the placement of parentheses in the provided code, with some participants expressing uncertainty about the correctness of the syntax.
  • A participant suggests that the OnTouch event handler needs additional code to actually load the new images, rather than just decrementing a counter.
  • One participant acknowledges the need for the addImages method to be reviewed to understand how new images are passed and suggests using breakpoints for debugging.

Areas of Agreement / Disagreement

Participants generally agree that the OnTouch event handler needs to be modified to include functionality for loading new images. However, there is no consensus on the correctness of the existing code or the intended logic behind it, leading to some uncertainty and differing interpretations.

Contextual Notes

There are unresolved questions regarding the implementation of the addImages method and whether the user interface requires explicit refreshing after images are loaded. The discussion highlights potential issues with code structure and clarity in the explanation of the intended functionality.

whitehorsey
Messages
188
Reaction score
0
On my app there are two rows that holds 3 images each (as seen in the image attachment). In the beginning, the 6 images are loaded and then when a person touches one of the images I want the other 6 images to be loaded. It's a series of choices. Ex. First 6 images would be to pick what day it is. After selecting the day, it would be replace the 6 images with 6 different choices for which month is it.

There is a series of choices in the series there is a list of the six choices that will be loaded.

images.jpg


I'm not sure how to use touchlistener so far I have this:

Code:
if(count > 1)
		{
			imageview.setOnTouchListener(new OnTouchListener()
			{
				
			    @Override
			    public boolean onTouch(View v, MotionEvent event) 
			    {
			        if(event.getAction() == MotionEvent.ACTION_UP)
			        {
			            count--;
			            return true;
			        }
			        return false;
			    }
			});
		}

If the there is more than one choice series (cause there will be some parts of the app where there won't be 6 new choices after selecting one), check to see if someone touched an image. However, the app right now loads the other choices from the series (day -> month) without waiting for someone to touch an image. How do I fix this?
 
Technology news on Phys.org
I know nothing about developing for Android nor in Java, but I know a thing or two about parentheses, and I am not convinced they look OK.
 
Borek said:
I know nothing about developing for Android nor in Java, but I know a thing or two about parentheses, and I am not convinced they look OK.
I thought that as well, but later decided they're probably OK. The OnTouchListener parameter

This part --
Code:
{
				
			    @Override
			    public boolean onTouch(View v, MotionEvent event) 
			    {
			        if(event.getAction() == MotionEvent.ACTION_UP)
			        {
			            count--;
			            return true;
			        }
			        return false;
			    }
			});
-- is an inline definition of the OnTouchListener method that is a parameter. The right brace on the last line matches the left brace at the top of what I copied. The right parenthesis matches the left parenthesis in setOnTouchListener(.

whitehorsey, your explanation of what you're trying to do is unclear.
whitehorsey said:
On my app there are two rows that holds 3 images each (as seen in the image attachment). In the beginning, the 6 images are loaded and then when a person touches one of the images I want the other 6 images to be loaded. It's a series of choices. Ex. First 6 images would be to pick what day it is. After selecting the day, it would be replace the 6 images with 6 different choices for which month is it.
After the 6 images are loaded and you touch one of the images, is the idea that 6 different images are to be loaded?

For something to happen when you touch one of the images, you need code in your OnTouch event handler to load the 6 new images. In your code, all that happens is that count is decremented.
 
Last edited:
Mark44 said:
is an inline definition of the OnTouchListener method that is a parameter. The right brace on the last line matches the left brace at the top of what I copied. The right parenthesis matches the left parenthesis in setOnTouchListener(.

Yes, it occurred to me that it can work this way, but I am not convinced it is the intent, it looks more like the code was pasted in the wrong place, producing code that is accidentally syntactically correct.

Or at least, in my code I would put parentheses differently, to not leave this "accidental correctness" impression.

But as I wrote, I know nothing about Java. I just started to learn yesterday.
 
whitehorsey, your explanation of what you're trying to do is unclear.
After the 6 images are loaded and you touch one of the images, is the idea that 6 different images are to be loaded?

Yep! I'm making a calendar so the first 6 images loaded are some random days of the week (mon, tues, etc.) . The person selects one of the days then it will load 6 random different images these will be to select the month.

For something to happen when you touch one of the images, you need code in your OnTouch event handler to load the 6 new images. In your code, all that happens is that count is decremented.

Thank You for catching that!

Code:
if(count > 1)
		{
			iv.setOnTouchListener(new OnTouchListener()
			{
				
			    @Override
			    public boolean onTouch(View v, MotionEvent event) 
			    {
			        if(event.getAction() == MotionEvent.ACTION_UP)
			        {
			        	count--;
			        	addImages(series.getList().get(index));
			            return true;
			        }
			        return false;
			    }
			});
		}

addImages passes the new 6 images. I tried touching the icons but nothing is happening.
 
whitehorsey said:
addImages passes the new 6 images. I tried touching the icons but nothing is happening.

I/anyone would need to see the addImages method to see how the method passes the images.

If you don't want to post it it's okay, but put breakpoints on the addImages method code and click on the cockroach (or ladybug) of eclipse. Sometimes it happens that whatever control one is using needs an explicit refresh (like ListViews sometimes, depending on how it is being used).
 
Last edited:

Similar threads

Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 49 ·
2
Replies
49
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K