Reloading contents of Listview frequently

Put your problem here if it does not fit any of the other categories.

Reloading contents of Listview frequently

Postby neha naik » Mon Mar 24, 2008 6:03 am


I am working on an application which implements google talk.
As soon as any contact comes online i need to change the gui of the Listview which i am using to display the contacts.
Like, if the contact comes online, that contact should be shown in green and then white once it goes offline.
I did read the solution given at the following urls, but could not understand where to put the thread code. ... -t537.html

I am pasting my code here, please help me understand the place where the additional code should be written.

Thanks in advance.


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;

import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainWindow extends Activity {

public ArrayList<String> messages = new ArrayList<String>();

private Handler mHandler = new Handler();

private LoginWindow mDialog;

private ListView mList;

public XMPPConnection connection;

private static final int ACTIVITY_CREATE = 0;

private HashMap<String, Presence> rosterData;

private Roster roster;

* Called with the activity is first created.
public void onCreate(Bundle icicle) {
mList = (ListView) this.findViewById(;

// Dialog for getting the xmpp settings
mDialog = new LoginWindow(this);

// Set a listener to show the settings dialog
Button setup = (Button) this.findViewById(;
// Show the setup dialog when user startup the application;

// Shows the setup dialog when user click on setup button
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) { Runnable() {
public void run() {
GtalkConnection g = new GtalkConnection();

// Show the new chat dialog on click on list
mList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {

* Called by login window when a connection is establised with the Gtalk
* server
* @param connection
* @throws InterruptedException
public void setConnection(final XMPPConnection connection) {
this.connection = connection;
roster = connection.getRoster();
roster.addRosterListener(new RosterListener() {

public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub

public void entriesDeleted(Collection<String> arg0) {
// TODO Auto-generated method stub

public void entriesUpdated(Collection<String> arg0) {
// TODO Auto-generated method stub

public void presenceChanged(Presence arg0) {
Log.i("GTalk", "Status @@@ " + arg0.getType() + " for "
+ arg0.getFrom());
String user = arg0.getFrom();
user = StringUtils.parseName(user);
updatePresence(user, arg0);

rosterData = new HashMap<String, Presence>();
for (RosterEntry entry : roster.getEntries()) {
.put(entry.getUser(), roster.getPresence(entry.getUser()));

private void updatePresence(String user, Presence presence) {
rosterData.remove(user + "");
rosterData.put(user + "", presence);

private void createRosterListData() {
// clear to avoid duplicate entries
if (messages != null && messages.size() > 0) {
Iterator<String> it = rosterData.keySet().iterator();
while (it.hasNext()) {
String user = (String);
Presence p = rosterData.get(user);
messages.add(user + "," + p.getType());

public XMPPConnection getConnection() {
return connection;

private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.multi_line_list_item, messages);

private void createChat(String name) {
Intent i = new Intent(this, ChatDialog.class);
i.putExtra("recipient", name);
startSubActivity(i, ACTIVITY_CREATE);

public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
GtalkConnection g = new GtalkConnection();
return true;
return false;
neha naik
Posts: 2
Joined: Fri Mar 21, 2008 11:30 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 22 guests