How can i create multiple windows / actives for each user in smack for android needed for chatting one on one what i had so far . After i login and click on the contacts tab the roster list is loaded then i proceed to message one of the contacts i click on the contact name where i go to the message activity sending message to the contact clicked works all i want to know is how to create a message list for each contact i want to chat with .
This is what some of my code looks like
Contacts.java
package co.fitcom.chatome;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.jivesoftware.smackx.vcardtemp.provider.VCardProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class Contacts extends Fragment {
ListView contactsListview;
View view;
VCard vCard = new VCard();
Presence presence;
//Roster UserNames
String rosterContactUsernames;
ArrayList<String> rosterContactsUsernameList = new ArrayList<String>();
String[] rosterUsernameString;
//Roster NickNames
String rosterContactNickNames;
String[] rosterContactNickNamesString;
ArrayList<String> rosterContactsNickNameList = new ArrayList<String>();
//Roster Avatars
byte [] rosterAvatar;
Bitmap [] rosterAvatarString;
ArrayList <Bitmap> rosterAvatarList = new ArrayList<Bitmap>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Roster roster = Login.connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for(RosterEntry entry : entries){
presence = roster.getPresence(entry.getUser());
Presence.Type type = presence.getType();
rosterContactUsernames = entry.getUser();
String rosterUserStatus = type.toString();
rosterContactsUsernameList.add(rosterContactUsernames);
try {
vCard.load(Login.connection , entry.getUser());
}
catch (SmackException.NoResponseException e){}
catch (XMPPException.XMPPErrorException e){}
catch (SmackException.NotConnectedException e){}
rosterContactNickNames = vCard.getNickName();
rosterContactsNickNameList.add(rosterContactNickNames);
rosterUsernameString = rosterContactsUsernameList.toArray(new String[rosterContactsUsernameList.size()]);
rosterContactNickNamesString = rosterContactsNickNameList.toArray(new String[rosterContactsNickNameList.size()]);
rosterAvatar = vCard.getAvatar();
System.out.println(Arrays.toString(rosterAvatar));
int rosterAvatarlen = rosterAvatar.length;
rosterAvatarList.add(BitmapFactory.decodeByteArray(rosterAvatar, 0,
rosterAvatarlen));
rosterAvatarString = rosterAvatarList.toArray(new Bitmap[rosterAvatarList.size()]);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_contacts , container,false);
contactsListview=(ListView)view.findViewById(R.id.rosterContactList);
ArrayAdapter <String> arrayAdapter = new ContactsArrayAdapter(this.getActivity() , rosterUsernameString ,rosterContactNickNamesString ,rosterAvatarString );
contactsListview.setAdapter(arrayAdapter);
contactsListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clickedContact = contactsListview.getItemAtPosition(position).toString();
System.out.println(contactsListview.getItemAtPosition(position).toString());
System.out.println(Login.connection.getUser());
String fromUser = Login.connection.getUser();
Intent intent = new Intent(getActivity() , Message.class);
intent.putExtra("clickcontactusername" , clickedContact);
intent.putExtra("fromuser" , fromUser);
intent.putExtras(intent);
startActivity(intent);
// System.out.println( rosterContactNickNamesString[contactsListview.get]);
System.out.println(contactsListview.getItemAtPosition(position).toString());
}
});
return view;
}
}
Message.java
package co.fitcom.chatome;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import java.util.ArrayList;
/**
* Created by osei.fortune on 02/10/2014.
*/
public class Message extends Activity {
Time time = new Time(Time.getCurrentTimezone());
private Integer timeDay;
private Integer timeMonth;
private Integer timeYear;
private String timeCurrentTime;
String [] timeMonthString = { "Jan" , "Feb" , "Mar", "Apr", "May" , "Jun" , "Jul" , "Aug","Sep","Oct","Nov" , "Dec"};
String timeMonthStr;
Intent intent ;
String contactCLickedOn;
ArrayList <String> messageList = new ArrayList<String>();
//Receiving Message
String messageFrom;
String messageThread;
//Sending Message
EditText messageBodyTextView;
String messageSendBody;
Button messageSendButton;
private String fromUser;
ListView messageListView;
///New Stuff
String messageSendFrom;
Bundle extras;
ArrayAdapter <String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
time.setToNow();
extras = getIntent().getExtras();
//intent = getIntent().getExtras();
timeDay = time.monthDay;
timeMonth = time.month;
timeYear = time.year;
timeCurrentTime = time.format("%k:%m");
if(timeMonth == 0){timeMonthStr = timeMonthString[0];}
else if(timeMonth == 1){timeMonthStr = timeMonthString[1];}
else if(timeMonth == 2){timeMonthStr = timeMonthString[2];}
else if(timeMonth == 3){timeMonthStr = timeMonthString[3];}
else if(timeMonth == 4){timeMonthStr = timeMonthString[4];}
else if(timeMonth == 5){timeMonthStr = timeMonthString[5];}
else if(timeMonth == 6){timeMonthStr = timeMonthString[6];}
else if(timeMonth == 7){timeMonthStr = timeMonthString[7];}
else if(timeMonth == 8){timeMonthStr = timeMonthString[8];}
else if(timeMonth == 9){timeMonthStr = timeMonthString[9];}
else if(timeMonth == 10){timeMonthStr = timeMonthString[10];}
else if(timeMonth == 11){timeMonthStr = timeMonthString[11];}
contactCLickedOn = extras.getString("clickcontactusername");
// contactCLickedOn = "admin@chatome.com";
fromUser = extras.getString("fromuser");
// fromUser = "triniwiz@chatome.com";
ActionBar actionBar = getActionBar();
actionBar.setTitle(contactCLickedOn);
messageBodyTextView = (EditText)findViewById(R.id.messageBody);
messageSendButton = (Button)findViewById(R.id.messageSendButton);
messageListView = (ListView)findViewById(R.id.messageListView);
adapter = new ArrayAdapter<String>(getApplicationContext() , android.R.layout.simple_list_item_2 ,android.R.id.text1 , messageList){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position,convertView,parent);
TextView textView1 = (TextView)view.findViewById(android.R.id.text1);
TextView textView2 = (TextView)view.findViewById(android.R.id.text2);
textView1.setText(messageSendBody);
textView2.setText(timeMonthStr +","+ timeDay + " "+ timeCurrentTime);
return view;
}
};
messageListView.setAdapter(adapter);
}
public void sendMessage(View v){
messageSendBody = messageBodyTextView.getText().toString();
if(messageSendBody != null){
//ChatManager chatManager = ChatManager.getInstanceFor(Login.connection);
Chat chat = Main.chatManager.createChat( contactCLickedOn, Main.messageListener);
try {chat.sendMessage(messageSendBody);}
catch (XMPPException E){}
catch (SmackException.NotConnectedException e){}
System.out.print(chat.getThreadID());
messageList.add(messageSendBody);
messageBodyTextView.setText("");
adapter.notifyDataSetChanged();
}
}
public Integer getTimeDay() {
return timeDay;
}
public Integer getTimeMonth() {
return timeMonth;
}
public String getTimeCurrentTime() {
return timeCurrentTime;
}
public Integer getTimeYear() {
return timeYear;
}
public String getFromUser() {
return fromUser;
}
}