Android<操练>:Gtalk(显示好友列表)
截图:

此处注意: 在操作这个之前,要在先前<登录>程序里面修改两处: 1. mGtalkSession =
xmppservice.createGTalkSession(login_User.getText().toString(),login_Pwd.getText().toString());
mGtalkSession.requestRoster(); 2. case 5: mGtalkSession.setPresence(new Presence(Im.PresenceColumns.AVAILABLE, "Am here now!")); createContact(); logMessage("the client requested roster from the server.");
代码:
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.Im;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class ContactsActivity extends Activity {
private static final String TAG="microChat.ContactsView";
private static final boolean DEBUG=true;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.contacts);
// Run a query against CONTENT_URI = "content://im/contacts"
Cursor cursor=managedQuery(Im.Contacts.CONTENT_URI, null,null, null, null);
TableLayout layout=(TableLayout)findViewById(R.id.contacts_table_layout);
int PRESENCE_STATUS=cursor.getColumnIndex(Im.PresenceColumns.PRESENCE_STATUS);
int NICKNAME=cursor.getColumnIndex(Im.ContactsColumns.NICKNAME);
int USERNAME=cursor.getColumnIndex(Im.ContactsColumns.USERNAME);
int numRows=cursor.count();
cursor.first();
for(int i=0;i<numRows;i++)
{ int presence=(int)cursor.getLong(PRESENCE_STATUS);
String name=cursor.getString(NICKNAME);
String jid=cursor.getString(USERNAME);
if(presence>0) layout.addView(row(new Contact(name,jid,presence)));
cursor.next();
}
}
class Contact
{ String name;
String jid;
int icon;
TextView text=null; // TODO: TEST
Contact(int i) // TEST
{ name="My Buddy "+i;
jid="Buddy"+i+"@jabber.net";
icon=R.drawable.icon;
}
Contact(String name,String jid,int presence)
{ this.name=" ("+presence+") "+name;
this.jid=jid;
switch(presence)
{
case Im.Presence.OFFLINE: icon=R.drawable.icon; break;
case Im.Presence.INVISIBLE: icon=R.drawable.icon; break;
case Im.Presence.DO_NOT_DISTURB: icon=R.drawable.icon; break;
case Im.Presence.AWAY: icon=R.drawable.icon; break;
case Im.Presence.IDLE: icon=R.drawable.icon; break;
case Im.Presence.AVAILABLE: icon=R.drawable.icon; break;
}
}
}
private TableRow row(final Contact contact)
{
TableRow template=(TableRow)findViewById(R.id.contacts_row_layout);
android.view.ViewGroup.LayoutParams layoutParams=template.getLayoutParams();
TableRow layout=new TableRow(this);
layout.setLayoutParams(layoutParams);
ImageView imageView=new ImageView(this);
imageView.setImageResource(contact.icon);
imageView.setPadding(2,6,2,0); // left, top, right, bottom
layout.addView(imageView);//,layoutParams);
final TextView text=new TextView(this);
text.setTag(contact); contact.text=text;
text.setText(contact.name);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setFocusable(true);
text.setOnClickListener(textClickListener);
layout.addView(text);//,layoutParams);
return(layout);
}
private OnClickListener textClickListener=new OnClickListener()
{ public void onClick(View view)
{ Contact contact=(Contact)view.getTag();
if(DEBUG) Log.d(TAG,"row: onClick: contactName="+contact.name);
}
};
}