开发者

TextView only updates once

I'm having a seemingly strange issue with my android app. Whether I call append or setText, my TextView will only update once.

I have my IME set to have a "Send" button that listens as follows:

        sendText.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(Te开发者_如何学编程xtView v, int actionId, KeyEvent event){
            if(actionId == EditorInfo.IME_ACTION_SEND){
                try {
                    send();
                    scroller.post(new Runnable() {
                        @Override
                        public void run() {
                            scroller.fullScroll(ScrollView.FOCUS_DOWN);
                        }
                    });
                } catch (Exception e) {
                    Log.e("chat", e.toString());
                }
            }
            return true;
        }
    });

The send method:

public void send(){
    final String message = sendText.getText().toString();
    final String ip = ipAddr.getText().toString();
    //rcvMsg.append("Me:  " + message + "\n");
    runOnUiThread(new Runnable(){
        public void run(){
            TextView rcv = (TextView)findViewById(R.id.rcvMsg);
            rcv.setText(rcv.getText()+"Me: "+message+"\n");
        }
    });
}

As you can see, I tried append and a setText in runOnUiThread. Both of those only update the textView the first time send() is called. On subsequent calls, it doesn't change.

But!

If I put the app in the background (hit home), then relaunch it, the TextView has all the proper text.

What am I missing?


I figured it out - it was a GUI issue..

I accidentally set the height of the TextView (inside a scrollview) to an exact value. Apparently, this caused the fullScroll method to screw up and not scroll down completely, so the added text was invisible. Setting the height of the TextView to wrap_content solved the problem.


Put below line of code after setContentView in your activity and remove from send method

TextView rcv = (TextView)findViewById(R.id.rcvMsg);

Change this line rcv.setText(rcv.getText()+"Me: "+message+"\n"); to

rcv.setText(rcv.getText().toString()+"Me: "+message+"\n");

in send method


You should not put

TextView rcv = (TextView)findViewById(R.id.rcvMsg);
rcv.setText(rcv.getText()+"Me: "+message+"\n");

In a runnable. Your code should look more like this.

public void send(){

    TextView rcv = (TextView)findViewById(R.id.rcvMsg);
    rcv.setText(rcv.getText()+"Me: "+sendText.getText().toString()+"\n");

}

If you are sending this message using the ip variable somehow you would do that inside of a runable, but setting the textarea inside of a runable is impossible seeing as the two are running on separate threads.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜