I am implementing a chatting application, where I have made a service to listen all the presence changed. On the change of the presence I want to update the data and I am unable to update the data that is showing in the expandable list view. Please suggest me a means to do the same.
public class UserMenuActivity extends ExpandableListActivity {
private XMPPConnection connection;
String name,availability,subscriptionStatus;
TextView tv_Status;
/** Variable Define here */
private String[] data = { "View my profile", "New Multiperson Chat",
"New Broad Cast Message", "New Contact Category", "New Group",
"Invite to CCM", "Search", "Expand All", "Settings", "Help",
"Close" };
private String[] data_Contact = { "Rename Category","Move Contact to Category", "View my profile",
"New Multiperson Chat", "New Broad Cast Message",
"New Contact Category", "New Group", "Invite to CCM", "Search",
"Expand All", "Settings", "Help", "Close" };
private String[] data_child_contact = { "Open chat", "Delete Contact","View my profile",
"New Multiperson Chat", "New Broad Cast Message",
"New Contact Category", "New Group", "Invite to CCM", "Search",
"Expand All", "Settings", "Help", "Close" };
private String[] menuItem = { "Chats", "Contacts", "CGM Groups", "Pending","Request" };
private List<String> menuItemList = Arrays.asList(menuItem);
private int commonGroupPosition = 0;
private String etAlertVal;
private DatabaseHelper dbHelper;
private int categoryID, listPos;
/** New Code here.. */
private ArrayList<String> groupNames;
private ArrayList<ArrayList<ChildItems>> childs;
private UserMenuAdapter adapter;
private Object object;
private String[] data2 = { "PIN Michelle", "IP Call" };
private ListView mlist2;
private ImageButton mimBtnMenu;
private LinearLayout mllpopmenu;
private View popupView;
private PopupWindow popupWindow;
private AlertDialog.Builder alert;
private EditText input;
private TextView mtvUserName, mtvUserTagLine;
private ExpandableListView mExpandableListView;
public static List<CategoryDataClass> categoryList;
private boolean menuType = false;
private String childValContact="";
public static Context context;
@Override
public void onBackPressed() {
if (mllpopmenu.getVisibility() == View.VISIBLE) {
mllpopmenu.setVisibility(View.INVISIBLE);
} else {
if (CCMStaticVariable.CommonConnection.isConnected()) {
CCMStaticVariable.CommonConnection.disconnect();
}
super.onBackPressed();
}
}
@SuppressWarnings("unchecked")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (mllpopmenu.getVisibility() == View.VISIBLE) {
mllpopmenu.setVisibility(View.INVISIBLE);
} else {
if (commonGroupPosition >= 4 && menuType == true) {
if(childValContact == ""){
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText,
data_Contact));
}else{
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText,
data_child_contact));
}
} else if (commonGroupPosition == 0) {
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText, data));
}
}
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.usermenulayout);
dbHelper = new DatabaseHelper(UserMenuActivity.this);
//this.context = context.getApplicationContext();
XMPPConn.getContactList();
connection = CCMStaticVariable.CommonConnection;
Presence userPresence = new Presence(Presence.Type.available);
userPresence.setPriority(24);
userPresence.setMode(Presence.Mode.away);
connection.sendPacket(userPresence);
}
@Override
protected void onResume() {
super.onResume();
Presence userPresence = new Presence(Presence.Type.available);
userPresence.setPriority(24);
userPresence.setMode(Presence.Mode.away);
connection.sendPacket(userPresence);
XMPPConn.getContactList();
setExpandableListView();
}
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (groupPosition == 1 && childPosition == 0) {
startActivity(new Intent(UserMenuActivity.this,
InvitetoCCMActivity.class));
} else if (groupPosition == 1 && childPosition != 0) {
Intent intent = new Intent(UserMenuActivity.this,
UserChatActivity.class);
intent.putExtra("userNameVal",
XMPPConn.mfriendList.get(childPosition - 1).friendName);
startActivity(intent);
} else if (groupPosition == 2 && childPosition == 0) {
startActivity(new Intent(UserMenuActivity.this,
CreateGroupActivity.class));
} else if (groupPosition == 2 && childPosition != 0) {
String GROUP_NAME = childs.get(groupPosition).get(childPosition)
.getName().toString();
int end = GROUP_NAME.indexOf("(");
CCMStaticVariable.groupName = GROUP_NAME.substring(0, end).trim();
startActivity(new Intent(UserMenuActivity.this,
GroupsActivity.class));
} else if (groupPosition >= 4) {
childValContact = childs.get(groupPosition).get(childPosition).getName().trim();
showToast("user==>"+childValContact, 0);
}
return false;
}
private void setExpandableListView() {
/***###############GROUP ARRAY ############################*/
final ArrayList<String> groupNames = new ArrayList<String>();
groupNames.add("Chats (2)");
groupNames.add("Contacts (" + XMPPConn.mfriendList.size() + ")");
groupNames.add("CGM Groups (" + XMPPConn.mGroupList.size() + ")");
groupNames.add("Pending (1)");
XMPPConn.getGroup();
categoryList = dbHelper.getAllCategory();
/**Group From Sever*/
if (XMPPConn.mGroupList.size() > 0) {
for (int g = 0; g < XMPPConn.mGroupList.size(); g++) {
XMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
groupNames.add(XMPPConn.mGroupList.get(g).groupName + "("
+ XMPPConn.mGroupContactList.size()+ ")");
}
}
if(categoryList.size() > 0){
for (int cat = 0; cat < categoryList.size(); cat++) {
groupNames.add(categoryList.get(cat).getCategoryName()+ "(0)");
}
}
this.groupNames = groupNames;
/*** ###########CHILD ARRAY * #################*/
ArrayList<ArrayList<ChildItems>> childs = new ArrayList<ArrayList<ChildItems>>();
ArrayList<ChildItems> child = new ArrayList<ChildItems>();
child.add(new ChildItems("Alisha", "Hi",0));
child.add(new ChildItems("Michelle", "Good Morning",0));
childs.add(child);
child = new ArrayList<ChildItems>();
child.add(new ChildItems("", "",0));
if (XMPPConn.mfriendList.size() > 0) {
for (int n = 0; n < XMPPConn.mfriendList.size(); n++) {
child.add(new ChildItems(XMPPConn.mfriendList.get(n).friendNickName,
XMPPConn.mfriendList.get(n).friendStatus,
XMPPConn.mfriendList.get(n).friendState));
}
}
childs.add(child);
/************** CGM Group Child here *********************/
child = new ArrayList<ChildItems>();
child.add(new ChildItems("", "",0));
if (XMPPConn.mGroupList.size() > 0) {
for (int grop = 0; grop < XMPPConn.mGroupList.size(); grop++) {
child.add(new ChildItems(
XMPPConn.mGroupList.get(grop).groupName + " ("
+ XMPPConn.mGroupList.get(grop).groupUserCount
+ ")", "",0));
}
}
childs.add(child);
child = new ArrayList<ChildItems>();
child.add(new ChildItems("Shuchi",
"Pending (Waiting for Authorization)",0));
childs.add(child);
/************************ Group Contact List *************************/
if (XMPPConn.mGroupList.size() > 0) {
for (int g = 0; g < XMPPConn.mGroupList.size(); g++) {
/** Contact List */
XMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
child = new ArrayList<ChildItems>();
for (int con = 0; con < XMPPConn.mGroupContactList.size(); con++) {
child.add(new ChildItems(
XMPPConn.mGroupContactList.get(con).friendName,
XMPPConn.mGroupContactList.get(con).friendStatus,0));
}
childs.add(child);
}
}
if(categoryList.size() > 0){
for (int cat = 0; cat < categoryList.size(); cat++) {
child = new ArrayList<ChildItems>();
child.add(new ChildItems("-none-", "",0));
childs.add(child);
}
}
this.childs = childs;
/** Set Adapter here */
adapter = new UserMenuAdapter(this, groupNames, childs);
setListAdapter(adapter);
object = this;
mlist2 = (ListView) findViewById(R.id.list2);
mimBtnMenu = (ImageButton) findViewById(R.id.imBtnMenu);
mllpopmenu = (LinearLayout) findViewById(R.id.llpopmenu);
mtvUserName = (TextView) findViewById(R.id.tvUserName);
mtvUserTagLine = (TextView) findViewById(R.id.tvUserTagLine);
//Set User name..
System.out.println("CCMStaticVariable.loginUserName==="
+ CCMStaticVariable.loginUserName);
if (!CCMStaticVariable.loginUserName.equalsIgnoreCase("")) {
mtvUserName.setText("" + CCMStaticVariable.loginUserName);
}
/** Expandable List set here.. */
mExpandableListView = (ExpandableListView) this
.findViewById(android.R.id.list);
mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
XMPPConn.getContactList();
if (parent.isGroupExpanded(groupPosition)) {
commonGroupPosition = 0;
}else{
commonGroupPosition = groupPosition;
}
String GROUP_NAME = groupNames.get(groupPosition);
int end = groupNames.get(groupPosition).indexOf("(");
String GROUP_NAME_VALUE = GROUP_NAME.substring(0, end).trim();
if (menuItemList.contains(GROUP_NAME_VALUE)) {
menuType = false;
CCMStaticVariable.groupCatName = GROUP_NAME_VALUE;
} else {
menuType = true;
CCMStaticVariable.groupCatName = GROUP_NAME_VALUE;
}
long findCatId = dbHelper.getCategoryID(GROUP_NAME_VALUE);
if (findCatId != 0) {
categoryID = (int) findCatId;
}
childValContact="";
showToast("Clicked on==" + GROUP_NAME_VALUE, 0);
return false;
}
});
/** Click on item */
mlist2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
if (commonGroupPosition >= 4) {
if(childValContact == ""){
if (pos == 0) {
showAlertEdit(CCMStaticVariable.groupCatName);
}
/** Move contact to catgory */
if (pos == 1) {
startActivity(new Intent(UserMenuActivity.this,AddContactCategoryActivity.class));
}
}else{
if(pos == 0){
Intent intent = new Intent(UserMenuActivity.this,UserChatActivity.class);
intent.putExtra("userNameVal",childValContact);
startActivity(intent);
}
if(pos == 1){
XMPPConn.removeEntry(childValContact);
showToast("Contact deleted sucessfully", 0);
Intent intent = new Intent(UserMenuActivity.this,UserMenuActivity.class);
}
}
} else {
/** MyProfile */
if (pos == 0) {
startActivity(new Intent(UserMenuActivity.this,
MyProfileActivity.class));
}
/** New multiperson chat start */
if (pos == 1) {
startActivity(new Intent(UserMenuActivity.this,
NewMultipersonChatActivity.class));
}
/** New Broadcast message */
if (pos == 2) {
startActivity(new Intent(UserMenuActivity.this,
NewBroadcastMessageActivity.class));
}
/** Click on add category */
if (pos == 3) {
showAlertAdd();
}
if (pos == 4) {
startActivity(new Intent(UserMenuActivity.this,
CreateGroupActivity.class));
}
if (pos == 5) {
startActivity(new Intent(UserMenuActivity.this,
InvitetoCCMActivity.class));
}
if (pos == 6) {
startActivity(new Intent(UserMenuActivity.this,
SearchActivity.class));
}
if (pos == 7) {
onGroupExpand(2);
for (int i = 0; i < groupNames.size(); i++) {
mExpandableListView.expandGroup(i);
}
}
/** Click on settings */
if (pos == 8) {
startActivity(new Intent(UserMenuActivity.this,
SettingsActivity.class));
}
if (pos == 10) {
System.exit(0);
}
if (pos == 14) {
if (mllpopmenu.getVisibility() == View.VISIBLE) {
mllpopmenu.setVisibility(View.INVISIBLE);
if (popupWindow.isShowing()) {
popupWindow.dismiss();
}
} else {
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(
UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText,
data));
}
}
}
}
});
}
/** Toast message display here.. */
private void showToast(String msg, int time) {
Toast.makeText(this, msg, time).show();
}
public String showSubscriptionStatus(String friend){
return friend;
}
}
Service.class
public class UpdaterService extends Service {
private XMPPConnection connection;
String Friend;
String user = "";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
showToast("My Service Started", 0);
connection = getConnection();
if (connection.isConnected()) {
final Roster roster = connection.getRoster();
RosterListener r1 = new RosterListener() {
@Override
public void presenceChanged(Presence presence) {
// TODO Auto-generated method stub
XMPPConn.getContactList();
}
@Override
public void entriesUpdated(Collection<String> arg0) {
// TODO Auto-generated method stub
//notification("entriesUpdated");
}
@Override
public void entriesDeleted(Collection<String> arg0) {
// TODO Auto-generated method stub
//notification("entriesDeleted");
}
@Override
public void entriesAdded(Collection<String> arg0) {
// TODO Auto-generated method stub
Iterator<String> it = arg0.iterator();
if (it.hasNext()) {
user = it.next();
}
RosterEntry entry = roster.getEntry(user);
if(entry.getType().toString().equalsIgnoreCase("to")){
int index_of_Alpha = Friend.indexOf("@");
String subID = Friend.substring(0, index_of_Alpha);
notification("Hi "+subID+" wants to add you");
}
}
};
if (roster != null) {
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
System.out.println("subscription going on");
roster.addRosterListener(r1);
}
} else {
showToast("Connection lost-", 0);
}
}
protected void showToast(String msg, int time) {
Toast.makeText(this, msg, time).show();
}
private XMPPConnection getConnection() {
return CCMStaticVariable.CommonConnection;
}
/** Notification manager */
private void notification(CharSequence message) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "CCM";
CharSequence contentText = message;
Intent notificationIntent = new Intent(this, ManageNotification.class);
notificationIntent.putExtra("Subscriber_ID",user );
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
}
Here is my adapter class
public class UserMenuAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groups;
private ArrayList<ArrayList<ChildItems>> childs;
private Context context;
public LayoutInflater inflater;
ImageView img_availabiliy;
private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET =
{android.R.attr.state_expanded};
private static final int[][] GROUP_STATE_SETS = {
EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};
public UserMenuAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<ChildItems>> childs) {
this.context = context;
this.groups = groups;
this.childs = childs;
inflater = LayoutInflater.from(context);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return (long) (groupPosition * 1024 + childPosition);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.child_layout, parent, false);
ChildItems ci = (ChildItems) getChild(groupPosition, childPosition);
TextView tv = (TextView) v.findViewById(R.id.tvChild);
tv.setText(ci.getName());
TextView tv2 = (TextView) v.findViewById(R.id.tvChild2);
tv2.setText(ci.getDailyStatus());
img_availabiliy = (ImageView)v.findViewById(R.id.img_childlayout_AVAILABILITY);
ImageView friendPics = (ImageView)v.findViewById(R.id.ivFriendPics);
if(ci.getStatusState() == 1){
img_availabiliy.setImageResource(R.drawable.online);
}
else if(ci.getStatusState()==0){
img_availabiliy.setImageResource(R.drawable.offline);
}
else if (ci.getStatusState()==2) {
img_availabiliy.setImageResource(R.drawable.away);
}
else if(ci.getStatusState()==3){
img_availabiliy.setImageResource(R.drawable.busy);
}
else{
img_availabiliy.setImageDrawable(null);
}
if((groupPosition == 1 && childPosition == 0)){
friendPics.setImageResource(R.drawable.inviteto_ccm);
img_availabiliy.setVisibility(View.INVISIBLE);
}
else if(groupPosition == 2 && childPosition == 0){
friendPics.setImageResource(R.drawable.new_ccmgroup);
img_availabiliy.setVisibility(View.VISIBLE);
}else{
if(ci.getPicture()!= null){
Bitmap bitmap = BitmapFactory.decodeByteArray(ci.getPicture(), 0, ci.getPicture().length);
bitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true);
friendPics.setImageBitmap(bitmap);
}else{
friendPics.setImageResource(R.drawable.avatar);
}
img_availabiliy.setVisibility(View.VISIBLE);
}
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return (long) (groupPosition * 1024);
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.group_layout, parent, false);
String gt = (String) getGroup(groupPosition);
TextView tv2 = (TextView) v.findViewById(R.id.tvGroup);
if (gt != null)
tv2.setText(gt);
/**Set Image on group layout, Max/min*/
View ind = v.findViewById( R.id.explist_indicator);
View groupInd = v.findViewById( R.id.llgroup);
if( ind != null ) {
ImageView indicator = (ImageView)ind;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
if( groupInd != null ) {
RelativeLayout indicator2 = (RelativeLayout)groupInd;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator2.setVisibility( View.INVISIBLE );
} else {
indicator2.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable2 = indicator2.getBackground();
drawable2.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
return v;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void onGroupCollapsed(int groupPosition) {
}
public void onGroupExpanded(int groupPosition) {
}
}
I just want to update my list in ON PRESENCE CHANGED method in the Service class.. Please suggest me a means to do the same.