开发者

Value in edittext don't show until it get focus. Android

I'm building an app that get its layout based on external data. The layout is split into different blocks depending on the indata. These blocks are displayed with the help of a view开发者_JS百科flipper. In each block there is, for the time being, one or more "textview" and "edittext". On the first page of the flipper all data is showing as it should in the textviews and edittexts. But on the other pages in the viewflipper the value in the edittexts is not showing until the edittext get focused. I have no idea why. So to be clear. The values for all edittexts is actually there, but doesn't show until the edittext get focused. The problem is the same on all devices I have runned the app on (emulator, HTC Desire, HTC Wildfire). Does anyone know how to fix this problem?

Here is the class that generate the layout:

public class ModuleLayout extends LinearLayout {

public ModuleLayout(Context context, ArrayList<BaseModuleItem> itemList) {
    super(context);
    TableLayout tempTable = new TableLayout(context);

        for (int i = 0; i < itemList.size(); i++)
        {
            TableRow tempRow = new TableRow(context);
            TextView tempTextView = new TextView(context);
            tempTextView.setText(itemList.get(i).getName());

            EditText tempEditText = new EditText(context);
            tempEditText.setText(itemList.get(i).getItemValue());

            tempRow.addView(tempTextView);
            tempRow.addView(tempEditText);

            tempTable.addView(tempRow);
        }

        tempTable.setColumnStretchable(1, true);
        this.addView(tempTable);
    }
}

Here is a picture of the problem in action.

Value in edittext don't show until it get focus. Android

The left picture displays all its values fine. The right picture is on the second position in the viewflipper and does not display the values in the edittexts, with the exception for the first that have focus. I should also mention that after an edittext has gotten focus it continues to display the value even if I fling to other views in the viewflipper and then back.


That Layout from how it appears here is not on the UI thread meaning no matter what you change you won't see anything until the UI thread (also known as the Main Thread) updates. The UI/Main thread is what all your activities run through so it's easy to think that stuff just updates automatically (since most of the previous work you have done is also somewhere within the Activity, I am assuming). The onFocusChanged event (clicking in the edittext) will update the UI thread by calling invalidate() on itself. You can force it to update by calling EditText.invalidate() if you need to, but it's usually best to have it update naturally after creation. Without seeing the rest of your code this is the best I can do for you, I hope this helps :)


Having not seeing your full code I'm making a bit of a guess-suggestion but have you tried textView.invalidate() (if calling from GUI thread) or textView.postInvalidate() (if calling from another thread)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜