我已经制作了一个gmail用户的聊天列表。代码有什么问题。当我运行此代码时,此代码不显示任何用户名。有时它会显示适配器内容已更改的错误,但ListView未收到通知。
public class CustomizedListView extends Activity {
static final String KEY_TITLE = "title";
static final String KEY_THUMB_URL = "thumb_url";
public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";
public static final String USERNAME = "user@gmail.com";
public static final String PASSWORD = "password";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> chatList = new ArrayList<HashMap<String, String>>();
ConnectionConfiguration connConfig = new ConnectionConfiguration(
HOST, PORT, SERVICE);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
connection.connect();
} catch (XMPPException ex) {
}
try {
connection.login(USERNAME, PASSWORD);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("XMPPChatDemoActivity",
"Logged in as " + connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
final Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
HashMap<String, String> map = new HashMap<String, String>();
Presence entryPresence = roster.getPresence(entry
.getUser());
Presence.Type type = entryPresence.getType();
map.put("USER", entry.getUser().toString());
map.put("STATUS", type.toString());
chatList.add(map);
}
roster.addRosterListener(new RosterListener() {
@Override
public void presenceChanged(Presence arg0) {
}
@Override
public void entriesUpdated(Collection<String> arg0) {
// TODO Auto-generated method stub
}
@Override
public void entriesDeleted(Collection<String> arg0) {
// TODO Auto-generated method stub
}
@Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
}
});
list = (ListView) findViewById(R.id.list);
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
这是我的ListAdater代码
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageView imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageView(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> chats= new HashMap<String, String>();
chats= data.get(position);
// Setting all values in listview
title.setText(chats.get("USER"));
if (chats.get("STATUS").equalsIgnoreCase("available"))
thumb_image.setImageResource(R.drawable.online);
else
thumb_image.setImageResource(R.drawable.ofline);
return vi;
}
}