ImageView setBackgroundRessource not update even with inflate

64 Views Asked by At

I'm trying to update an imageView from a gridview with a border, for doing this I have this xml file which works :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item> 
        <shape android:shape="rectangle">
            <solid android:color="#158CDB" /> 
        </shape>
    </item>   
    <item android:left="3dp" android:right="3dp" android:top="3dp" android:bottom="3dp">  
        <shape android:shape="rectangle"> 
            <solid android:color="#FFFFFF" />
        </shape>
    </item>    
</layer-list> 

So I launch my Activity, a timer is launched and after 15 secondes, I try to update an imageView, to do so I tried this code :

public void highlightFoodItem(int id) {
    ImageAdapter ia = (ImageAdapter) _grid.getAdapter();
    for (int i = 0; i < ia.getCount(); i++) {
        View currentView = ia.getView(i, null, _grid);
        if(id == Integer.valueOf(currentView.getTag().toString().trim())) {
            currentView.setBackgroundResource(R.drawable.border);
            currentView.invalidate();
        }   
    }
}

So grid is of course the GridView that I'm using, I checked I enter in the if but nothing happend and I don't see why... It's also the right view, I have the good position.

If you need the timer which launch my function :

public void timer(int duration, int tick) {
    _timer = new CountDownTimer(duration, tick) {

        public void onTick(long millisUntilFinished) {
            _isRunning = true;
        }

        public void onFinish() {
            _isRunning = false;
            String currentMeal = getMealType();
            int currentCounter = -1;
            int currentFoodItemId = -1;

            for (int i = 0; i < _user.get_preferences().size(); i++) {
                Historic temp = _user.get_preferences().get(i);
                String[] hMealType = temp.getType().split("_"); 

                if(currentMeal.equals(hMealType[1]) && hMealType[0].equals("preferences")) {
                    if(temp.getCounter() > currentCounter && !_foodlist.selectListContainsFood(temp.getFooditemid())) {
                        currentFoodItemId = temp.getFooditemid();
                        currentCounter = temp.getCounter();
                    }
                }
            }

            _prompt.setText("> Maybe you should take a(n) "+_foodlist.getFoodItemName(currentFoodItemId)+" with it.");

            final int id = currentFoodItemId;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    highlightFoodItem(id);                      
                }
            });
        }
    };
    _timer.start();
}

If you can help me...

Best regards, zed13

Ps : forgive my poor english x)

1

There are 1 best solutions below

0
On BEST ANSWER

Ok, I try to update the view that I got from the image adapter, but If I use the function getChildAt from the gridView I succeed to do what I My function now :

public void highlightFoodItem(int id) {
    ImageAdapter ia = (ImageAdapter) _grid.getAdapter();
    for (int i = 0; i < ia.getCount(); i++) {
        View currentView = ia.getView(i, null, _grid);
        if(id == Integer.valueOf(currentView.getTag().toString().trim())) {
            _grid.getChildAt(i).setBackgroundResource(R.drawable.border_cyan);          
        }   
    }
}