开发者

I would like to know how to implement a layout that has static labels and dynamic data underneath them on my android phone

开发者 https://www.devze.com 2023-02-16 15:55 出处:网络
I am working on an app right now that takes in data from inputstream and displays it, but I would like to know, specifically in XML, how or where, I can learn to create a display to the user that show

I am working on an app right now that takes in data from inputstream and displays it, but I would like to know, specifically in XML, how or where, I can learn to create a display to the user that shows static labels with the data coming in from the stream underneath them, constantly being updated. I believe I want to use a relative layout. The bold letters are the static labels, and the italics are the dynamic values that I get from the stream. Please help. Thank you.

Example: Temperature Volts Tachomete开发者_运维技巧r

           *20*    *12*   *4000*


forgive me if my answer is not what you were hoping, because I can't think of any way to do this purely in XML. But, until a better answer comes along, here's how I've been doing a similar thing:

In the activity that is going to be displaying the results, let there be a Handler object, and private Runnable classes which each update an element of the UI.

/** Handler for callbacks to the UI thread */
final Handler mHandler = new Handler();

/** Runnable class to be posted in handler */
private class SetTemperatureText implements Runnable {
    private final int newTemp;
    public SetTemperatureText(int newTemp) {
        this.newTemp = newTemp;
    }
    public void run() {
        mTemperatureTextView.setText(newTemp + " degrees C");
    }
}

You could have one of these for each dynamically changing field.

Elsewhere in the same activity, you spawn a new thread to deal with the incoming input. Whenever it obtains a new value for one of the fields, have it create a new SetWhateverField object with the new value and post it to the Handler. The UI thread will shortly come along and update the UI with the new value.

As for the layout itself, a RelativeLayout doesn't sound like a bad idea. A TableLayout might work well, too: http://developer.android.com/resources/tutorials/views/hello-tablelayout.html

It was hard for me to tell whether you needed more information on xml/layout structure or on methods of dynamically updating UI elements. If you clarify this, maybe I (and everyone else) will be able to help you more.

0

精彩评论

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