Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setText is not updating

Tags:

android

Here is the code and i want to setText the Array of Textview after the random Images selected.. its not updating the textVew. I am passing Context and view to the constructor of this class of my other activity ? this is non activity class how to achieve the same?

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

how to setText the textview ?

like image 385
user1169079 Avatar asked Mar 07 '12 11:03

user1169079


1 Answers

for anyone who is having this problem, you may have to place your setText method inside this

    ((Activity) context).runOnUiThread(new Runnable() { 
        @Override
        public void run() {
            ((EditText)editText).setText(text);
        }
    });
like image 156
Gplex314159 Avatar answered Nov 15 '22 21:11

Gplex314159