setText没有更新

时间:2012-03-07 11:29:42

标签: android

这是代码,我想在选择随机图像之后将TextText设置为Textview数据..它不会更新textVew。我传递Context并查看我的其他活动的这个类的构造函数?这是非活动类如何实现相同的?

public class DisplayObject {
    Random _randomGenerator = new Random();
    int hudDisplay[] = {
        R.id.HUD_Text_Element1,
        R.id.HUD_Text_Element2,
        R.id.HUD_Text_Element3,
        R.id.HUD_Text_Element4,
        R.id.HUD_Text_Element5,
        R.id.HUD_Text_Element6,
        R.id.HUD_Text_Element7
    };

    static View[] _hiddenObjects;
    int NO_OF_OBJECTS = 15;
    TextView[] _HudTextDisplay = new TextView[7];

    public HiddenObject(Context context, View v) {
        // TODO Auto-generated constructor stub

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View huddisplay = inflater.inflate(R.layout.gameplay, null);
        /*NO_OF_OBJECTS = context.getResources().getInteger(R.integer.NO_OF_OBJECTS_AVAILABLE_PER_LEVEL);*/
        _hiddenObjects = new View[NO_OF_OBJECTS];
        random();

        for (int i = 0; i < hudDisplay.length; ++i) {
            _HudTextDisplay[i] = (TextView) huddisplay.findViewById(hudDisplay[i]);
            _HudTextDisplay[i].setText(_hiddenObjects[i].getTag().toString());
        }
    }

    public void random() {
        List<Integer> generated = new ArrayList<Integer>();
        for (int i = 0; i < NO_OF_OBJECTS; ++i) {
            while (true) {
                Integer next = _randomGenerator.nextInt(GamePlayActivity._hiddenObjects.length);
                if (!generated.contains(next)) {
                    _hiddenObjects[i] = GamePlayActivity._hiddenObjects[next];
                    Log.d("Check", "Image is " + _hiddenObjects[i].getTag());

                    generated.add(next);
                    break;
                }
            }
        }
    }
}

如何设置文本视图?

2 个答案:

答案 0 :(得分:8)

对于遇到此问题的任何人,您可能必须将setText方法放在此

    ((Activity) context).runOnUiThread(new Runnable() { 
        @Override
        public void run() {
            ((EditText)editText).setText(text);
        }
    });

答案 1 :(得分:0)

您应该在 postInvalidate()之后拨打setText(...) ,而不是提前!

 ...
 _HudTextDisplay[i].setText(_hiddenObjects[i].getTag().toString());
 _HudTextDisplay[i].postInvalidate();
 ...