Why my button can trigger the UI to scroll and my TimerTask inside the activity can't?
Posted
by Spidey
on Stack Overflow
See other posts from Stack Overflow
or by Spidey
Published on 2010-05-19T14:55:41Z
Indexed on
2010/05/21
21:10 UTC
Read the original article
Hit count: 471
Long Story Short: a method of my activity updates and scrolls the ListView through an ArrayAdapter like it should, but a method of an internal TimerTask for polling messages (which are displayed in the ListView) updates the ListView, but don't scroll it. Why?
Long Story:
I have a chat activity with this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
<ListView android:id="@+id/messageList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:fadeScrollbars="true"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<EditText android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
/>
</LinearLayout>
</LinearLayout>
The internal listView (with id messageList) is populated by an ArrayAdapter which inflates the XML below and replaces strings in it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:background="#fff"
android:paddingLeft="2dp"
android:paddingRight="2dp"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#00F"
android:typeface="monospace"
android:text="2010-10-12 12:12:03"
android:gravity="left"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sender"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#f84"
android:text="spidey"
android:gravity="right"
android:textStyle="bold"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="1dp"
android:gravity="left"
android:layout_below="@id/date"
android:text="Mensagem muito legal 123 quatro cinco seis."
android:textColor="#000"
/>
</RelativeLayout>
The problem is: in the main layout, I have a EditText for the chat message, and a Button to send the message. I have declared the adapter in the activity scope:
public class ChatManager extends Activity{
private EditText et;
private ListView lv;
private Timestamp lastDate = null;
private long campaignId;
private ChatAdapter ca;
private List<ChatMessage> vetMsg = new ArrayList<ChatMessage>();
private Timer chatPollingTimer;
private static final int CHAT_POLLING_PERIOD = 10000;
...
}
So, inside sendMessage(View v), the notifyDataSetChanged() scrolls the ListView acordingly, so I can see the latest chat messages automatically:
public void sendMessage(View v) {
String msg = et.getText().toString();
if(msg.length() == 0){
return;
}
et.setText("");
String xml = ServerCom.sendAndGetChatMessages(campaignId, lastDate, msg);
Vector<ChatMessage> vetNew = Chat.parse(new InputSource(new StringReader(xml)));
//Pegando a última data
if(!vetNew.isEmpty()){
lastDate = vetNew.lastElement().getDateSent();
//Atualizando a tela
vetMsg.addAll(vetNew);
ca.notifyDataSetChanged();
}
}
But inside my TimerTask, I can't. The ListView IS UPDATED, but it just don't scroll automatically. What am I doing wrong?
private class chatPollingTask extends TimerTask {
@Override
public void run() {
String xml;
if(lastDate != null){
//Chama o Updater
xml = ServerCom.getChatMessages(campaignId, lastDate);
}else{
//Chama o init denovo
xml = ServerCom.getChatMessages(campaignId);
}
Vector<ChatMessage> vetNew = Chat.parse(new InputSource(new StringReader(xml)));
if(!(vetNew.isEmpty())){
//TODO: descobrir porque o chat não está rolando quando chegam novas mensagens
//Descobrir também como forçar o rolamento, enquanto o bug não for corrigido.
Log.d("CHAT", "New message(s) acquired!");
lastDate = vetNew.lastElement().getDateSent();
vetMsg.addAll(vetNew);
ca.notifyDataSetChanged();
}
}
}
How can I force the scroll to the bottom? I've tried using scrollTo using lv.getBottom()-lv.getHeight(), but didn't work. Is this a bug in the Android SDK?
Sorry for the MASSIVE amount of code, but I guess this way the question gets pretty clear.
© Stack Overflow or respective owner