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

Hi,

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.
http://www.anddev.org/refreshing_conten ... -t537.html
http://www.anddev.org/viewtopic.php?p=2056#2056

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

Thanks in advance.
Neha.

package org.apache.android.xmpp;

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.app.Activity;
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.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mList = (ListView) this.findViewById(R.id.listMessages);
setListAdapter();

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

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

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

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



/**
* 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()) {
rosterData
.put(entry.getUser(), roster.getPresence(entry.getUser()));
}
createRosterListData();
setListAdapter();
}

private void updatePresence(String user, Presence presence) {
rosterData.remove(user + "@gmail.com");
rosterData.put(user + "@gmail.com", presence);
createRosterListData();
}

private void createRosterListData() {
// clear to avoid duplicate entries
if (messages != null && messages.size() > 0) {
messages.clear();
}
Iterator<String> it = rosterData.keySet().iterator();
while (it.hasNext()) {
String user = (String) it.next();
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);
mList.setAdapter(adapter);
mList.getBottom();
}

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();
g.logout();
return true;
}
return false;
}
}
neha naik
Freshman
Freshman
 
Posts: 2
Joined: Fri Mar 21, 2008 11:30 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 19 guests