ImageView setBackgroundRessource 即使使用 inflate 也不会更新

ImageView setBackgroundRessource not update even with inflate

我正在尝试从带边框的 gridview 更新 imageView,为此,我有这个有效的 xml 文件:

<?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> 

所以我启动了我的 Activity,启动了一个计时器,在 15 秒后,我尝试更新一个 imageView,为此我尝试了以下代码:

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();
        }   
    }
}

所以网格当然是我正在使用的 GridView,我检查了我是否输入了 if 但什么也没发生,我不明白为什么......这也是正确的视图,我有很好的位置。

如果您需要启动我的功能的计时器:

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();
}

如果你能帮我...

此致, zed13

Ps : 请原谅我糟糕的英语 x)

好的,我尝试更新从图像适配器获得的视图,但是如果我使用 gridView 中的函数 getChildAt,我现在可以成功执行我的函数:

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);          
        }   
    }
}