Jennifer
Jennifer

Reputation: 1852

Unable to the the Image to the desired position in android's custom listview

Recently, I've managed to make a custom listview in android that displays the image in the list. However, I have one problem that I cannot solve. What I tried to do is fairly simple, 1. display the blinking icon to the list item that has a same date as the json data via web. 2. Display the image for the 2nd and the 3rd position of the listview. 3. And for the rest of the list items, there will be no images shown.

I have a simple codition in the Custom adapter's getView method that actually compares the current date with the date that gets retreived from the json data via web. I've set the function that I'm using below.

If the current date is equal to the date from the json data, it will display a image that blinks(For testing purposes I'm using 2015/05/14). if position is equal to the 2nd or 3 rd position display a icon. Else show nothing. However, some images shows up at random positions of the listview. Is there some bug in my code? How is it possible to not show the icons that doesn't meet the conditions ?

if (days.get(position).trim().toString().equals("2015/05/14")||days.get(position).trim().toString()=="2015/05/14") {

    holder.left.setBackgroundResource(R.drawable.blinker);
    AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
            .getBackground();

    // Start the animation (looped playback by default).
    frameAnimation.start();

}else{
     holder.left.setImageResource(android.R.color.transparent);
}

if(position==1||position==2){

    holder.left.setImageResource(R.drawable.new1);

}else{
     holder.left.setImageResource(android.R.color.transparent);
}

if(position==getCount()-1){
     holder.left.setImageResource(android.R.color.transparent);
}



return convertView;
}

Upvotes: 1

Views: 61

Answers (1)

Kushal
Kushal

Reputation: 8478

if (days.get(position).trim().toString().equals("2015/05/14")||days.get(position).trim().toString()=="2015/05/14") {

    holder.left.setBackgroundResource(R.drawable.blinker);
    AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
        .getBackground();

    // Start the animation (looped playback by default).
    frameAnimation.start();

    // set variable with index of blinking row
    mBlinkingIndex = position;



   }else{
     holder.left.setImageResource(android.R.color.transparent);
   }

In subsequent calls of getView(), we use mBlinkingIndex to determine.

Because we need to set 2nd and 3rd row with drawable new1

    if((position = (mBlinkingIndex+1) || (position == (mBlinkingIndex+2)){
        // this row is special
        holder.left.setImageResource(R.drawable.new1);

    }else{
        // this row is not special
         holder.left.setImageResource(android.R.color.transparent);
    }

So, getView() will look like :

    if (days.get(position).trim().toString().equals("2015/05/14")||days.get(position).trim().toString()=="2015/05/14") {

    holder.left.setBackgroundResource(R.drawable.blinker);
    AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
        .getBackground();

    // Start the animation (looped playback by default).
    frameAnimation.start();

    // set variable with index of blinking row
    mBlinkingIndex = position;



   }else{
     holder.left.setImageResource(android.R.color.transparent);
   }

   if((position = (mBlinkingIndex+1) || (position == (mBlinkingIndex+2)){
        // this row is special
        holder.left.setImageResource(R.drawable.new1);

    }else{
        // this row is not special
         holder.left.setImageResource(android.R.color.transparent);
    }

Upvotes: 1

Related Questions