Imageviews dissappears when scroll out of view - android

I am going really crazy about this - Ok its a listview - two imageviews - to the left all listview-items must have one image each - that is all to the left are populated. But - to the right I only wants to populate 3 listitems with an imageview. And I have learned, due to recycling that the other ones (that I Do not want to be populated) must be set to invisible. What happends - when launching the app - the 3 imageviews to the right is first seen - BUT, when scrolling them out of view and then back - they are all gone.
very greatful for help
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.main, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.label);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
holder.infoView = (ImageView) convertView.findViewById(R.id.image_icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(values[position]);
switch (position) {
case 0:
holder.imageView.setImageResource(R.drawable.conv);
holder.infoView.setVisibility(View.GONE);
break;
case 1:
holder.imageView.setImageResource(R.drawable.counting);
holder.infoView.setVisibility(View.GONE);
break;
case 2:
holder.imageView.setImageResource(R.drawable.travelling);
holder.infoView.setVisibility(View.GONE);
break;
case 3:
holder.imageView.setImageResource(R.drawable.dating);
holder.infoView.setVisibility(View.GONE);
break;
case 4:
holder.imageView.setImageResource(R.drawable.restaurant);
holder.infoView.setVisibility(View.GONE);
break;
case 5:
holder.imageView.setImageResource(R.drawable.thaidishes);
holder.infoView.setVisibility(View.GONE);
break;
case 6:
holder.imageView.setImageResource(R.drawable.time_);
holder.infoView.setImageResource(R.drawable.imageicon);
break;
case 7:
holder.imageView.setImageResource(R.drawable.time2);
holder.infoView.setVisibility(View.GONE);
break;
case 8:
holder.imageView.setImageResource(R.drawable.colours);
holder.infoView.setVisibility(View.GONE);
break;
case 9:
holder.imageView.setImageResource(R.drawable.weather);
holder.infoView.setVisibility(View.GONE);
break;
case 10:
holder.imageView.setImageResource(R.drawable.directions);
holder.infoView.setVisibility(View.GONE);
break;
case 11:
holder.imageView.setImageResource(R.drawable.emergency);
holder.infoView.setVisibility(View.GONE);
break;
case 12:
holder.imageView.setImageResource(R.drawable.zoo);
holder.infoView.setVisibility(View.GONE);
break;
case 13:
holder.imageView.setImageResource(R.drawable.shopping);
holder.infoView.setVisibility(View.GONE);
break;
case 14:
holder.imageView.setImageResource(R.drawable.bank);
holder.infoView.setVisibility(View.GONE);
break;
case 15:
holder.imageView.setImageResource(R.drawable.hotel);
holder.infoView.setVisibility(View.GONE);
break;
case 16:
holder.imageView.setImageResource(R.drawable.countries);
holder.infoView.setVisibility(View.GONE);
break;
case 17:
holder.imageView.setImageResource(R.drawable.cities);
holder.infoView.setImageResource(R.drawable.imageicon);
break;
case 18:
holder.imageView.setImageResource(R.drawable.features);
holder.infoView.setImageResource(R.drawable.imageicon);
break;
}
return convertView;
}

I can't stress enough that an 18 case switch is bad style and very hard to work with, and also that method is way too long. You should rethink the way your code is constructed - break it into more methods or classes.
As for your problem, you're views are being recycled, so once you set them to gone - they're gone.
You're just missing the line: holder.infoView.setVisibility(View.VISIBLE); in cases 6, 17 and 18.

You're setting the ImageView's visibility to GONE when recycling, but not resetting it to VISIBLE for the elements that have the second image. Add this to cases where the second image should show up:
holder.infoView.setVisibility(View.VISIBLE);

Related

Hide ImageView hides multiple images

I'm trying to hide a specific die after it has been selected and used.
However, it keeps hiding all the ImageView below the one select
I've tried using different containers but can't seem to figure it out
This is XML for the dice for 3 of the dice:
<ImageView
android:id="#+id/img4"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="Select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline3"
app:srcCompat="?attr/colorControlHighlight" />
<ImageView
android:id="#+id/img3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="Select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline2"
app:srcCompat="?attr/colorControlHighlight" />
<ImageView
android:id="#+id/img1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="Select"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="?attr/colorControlHighlight" />
<androidx.constraintlayout.widget.Guideline
This is the method used to hide the dice:
public void RemoveDice(int pos) {
switch (pos) {
case 0:
img1.setVisibility(View.INVISIBLE);
case 1:
img2.setVisibility(View.GONE);
case 2:
img3.setVisibility(View.GONE);
case 3:
img4.setVisibility(View.INVISIBLE);
case 4:
img5.setVisibility(View.GONE);
case 5:
img6.setVisibility(View.GONE);
case 6:
img7.setVisibility(View.GONE);
case 7:
img8.setVisibility(View.INVISIBLE);
case 8:
img9.setVisibility(View.GONE);
}
}
This shows before selecting Blue 6
https://prnt.sc/peufqr
This after the selected die has been removed
https://prnt.sc/peugn7
I think my problem lies somewhere in the XML file but I am unsure what containers to use to prevent this.
You are missing break between each case. When using a switch, you have to add a break. Otherwise, it will execute any code until it reaches the end of the switch statement.
public void RemoveDice(int pos) {
switch (pos) {
case 0:
img1.setVisibility(View.INVISIBLE);
break; // Add this
case 1:
img2.setVisibility(View.GONE);
break; // Add this
case 2:
img3.setVisibility(View.GONE);
break; // Add this
case 3:
img4.setVisibility(View.INVISIBLE);
break; // Add this
case 4:
img5.setVisibility(View.GONE);
break; // Add this
case 5:
img6.setVisibility(View.GONE);
break; // Add this
case 6:
img7.setVisibility(View.GONE);
break; // Add this
case 7:
img8.setVisibility(View.INVISIBLE);
break; // Add this
case 8:
img9.setVisibility(View.GONE);
break; // Add this
}
}
You are not breaking switch case. use break statement
public void RemoveDice(int pos)
{
switch (pos)
{
case 0:
img1.setVisibility(View.INVISIBLE);
break;
case 1:
img2.setVisibility(View.GONE);
break;
case 2:
img3.setVisibility(View.GONE);
break;
case 3:
img4.setVisibility(View.INVISIBLE);
break;
case 4:
img5.setVisibility(View.GONE);
break;
case 5:
img6.setVisibility(View.GONE);
break;
case 6:
img7.setVisibility(View.GONE);
break;
case 7:
img8.setVisibility(View.INVISIBLE);
break;
case 8:
img9.setVisibility(View.GONE);
break;
}
}
Add breaks after every statement.
public void RemoveDice(int pos) {
switch (pos) {
case 0:
img1.setVisibility(View.INVISIBLE);
break; // Add this
case 1:
img2.setVisibility(View.GONE);
break; // Add this
case 2:
img3.setVisibility(View.GONE);
break; // Add this
case 3:
img4.setVisibility(View.INVISIBLE);
break; // Add this
case 4:
img5.setVisibility(View.GONE);
break; // Add this
case 5:
img6.setVisibility(View.GONE);
break; // Add this
case 6:
img7.setVisibility(View.GONE);
break; // Add this
case 7:
img8.setVisibility(View.INVISIBLE);
break; // Add this
case 8:
img9.setVisibility(View.GONE);
break; // Add this
}
}

Adding touch pointers in an android app

I'd like to add more than two pointers.
Here is the current code that works:
if (id == 0)
textView1.setText(touchStatus);
else
textView2.setText(touchStatus);
How would I add a third, forth, fifth...?
switch(id) {
case 0:
textView1.setText(touchStatus);
break;
case 1:
textView2.setText(touchStatus);
break;
case 2:
textView3.setText(touchStatus);
break;
case 3:
textView4.setText(touchStatus);
break;
...
}

Android - Unexpected layout

i haven't many experience in android development. this is my code:
public void onClick(View v) {
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
}
myAvatar is an ImageView.
when i click in one of the four imageButton , the result is always the same: Case R.id.categoriaA4.
Why??
You need to add break to each case. like
switch(v.getId()){
case R.id.categoriaA1:
myAvatar.setBackgroundResource(R.drawable.cata1);
break;
case R.id.categoriaA2:
myAvatar.setBackgroundResource(R.drawable.cata2);
break;
case R.id.categoriaA3:
myAvatar.setBackgroundResource(R.drawable.cata3);
break;
case R.id.categoriaA4:
myAvatar.setBackgroundResource(R.drawable.cata4);
break;
}

switch case inside other switch case

i have a expandable listview. when selecting an item from this list, childd and parentt variables change. it works fine. but after that, the code below is written, but only last cases work :
switch (groupp)
{
case 1:
{
switch (childd)
{
case 1:
{SelectedGenre.setText("1") ;}
case 2:
{SelectedGenre.setText("2") ;}
case 3:
{SelectedGenre.setText("3") ;}
case 4:
{SelectedGenre.setText("4") ; }
case 5:
{SelectedGenre.setText("5") ;}
}
}
case 2:
{
switch (childd)
{
case 1:
{SelectedMozoo.setText("a") ;}
case 2:
{SelectedMozoo.setText("b") ;}
case 3:
{SelectedMozoo.setText("c") ;}
case 4:
{SelectedMozoo.setText("d") ; }
case 5:
{SelectedMozoo.setText("e") ; }
case 6:
{SelectedMozoo.setText("f") ; }
}
}
}
after running this code, SelectedGenre sets to : 5 and selected mozoo sets to : f. but groupp is not 1 and childd5
use break statement after each case complete.
because switch case says that if any of case is matched rest all will be executed if there is no termination. so use break statement after each case;
You don't have break in your switch case.
Try this:
switch (groupp)
{
case 1:
{
switch (childd)
{
case 1:
{SelectedGenre.setText("1") ;break;}
case 2:
{SelectedGenre.setText("2") ;break;}
case 3:
{SelectedGenre.setText("3") ;break;}
case 4:
{SelectedGenre.setText("4") ; break;}
case 5:
{SelectedGenre.setText("5") ;break;}
}
}
break;
case 2:
{
switch (childd)
{
case 1:
{SelectedMozoo.setText("a") ;break;}
case 2:
{SelectedMozoo.setText("b") ;break;}
case 3:
{SelectedMozoo.setText("c") ;break;}
case 4:
{SelectedMozoo.setText("d") ; break;}
case 5:
{SelectedMozoo.setText("e") ; break;}
case 6:
{SelectedMozoo.setText("f") ; break;}
}break;
}
}

Why I am not able to getProper Text Value while Viewing Gallery in my application?

In My Application i have Implement the Simple Gallery Demo.
Here i am going to set text Value according to user viewing the gallery.
Here is my Code for GetView method of Image Adapter class:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(300, 300));
switch(position){
case 0:
productName.setText("dimond540_1");
break;
case 1:
productName.setText("diamond540_2");
break;
case 2:
productName.setText("diamond_ink_bottle_1");
break;
case 3:
productName.setText("diamond_ink_bottle_2");
break;
case 4:
productName.setText("diamond_ink_bottle_3");
break;
case 5:
productName.setText("micarta_1");
break;
case 6:
productName.setText("micarta_2");
break;
case 7:
productName.setText("vac700_1");
break;
case 8:
productName.setText("vac700_2");
break;
}
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
Now the Proble is, While i am viewing the gallery image, text value is change. But i am not able to get the textValue that i have set as second position and Second last position.
I have tryed many but every time i got all value but not the second position textValue and SecondLast position textValue.
So, whats wrong in my code?
I have solve My Question as below:
I have made the Method in that class and in that method i change the TextValue and it works great.
Code:
public long getItemId(int position) {
switch(position){
case 0:
productName.setText("Dimond540(1)");
break;
case 1:
productName.setText("Dimond540(2)");
break;
case 2:
productName.setText("Diamond Ink Bottle(1)");
break;
case 3:
productName.setText("Diamond Ink Bottle(2)");
break;
case 4:
productName.setText("Diamond Ink Bottle(3)");
break;
case 5:
productName.setText("Micarta(1)");
break;
case 6:
productName.setText("Micarta(2)");
break;
case 7:
productName.setText("Vac700(1)");
break;
case 8:
productName.setText("Vac700(2)");
break;
}
return position;
}

Categories

Resources