ExpandableList Auto refresh without touch

Put your problem here if it does not fit any of the other categories.

ExpandableList Auto refresh without touch

Postby srinuv28 » Mon Nov 30, 2009 3:02 am

Hi...

I am implementing IM(Instant Messenger), here I am using ExpandableList for friends, separating online and offline friends using groups..

my requirement is I am getting latest friend list from the server every 3 seconds, but these new values are not updated in ExpandableList.(If I expand and collapse the group by touching then the values are updated), without touching values are not updated..

please any one help on this..

please find the attached screen shot..

my code is
******************************************************************************************************************************************************************
package com.mtouche.android.juzfrens;

import java.util.HashMap;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnKeyListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;

import com.mtouche.android.juzfrens.util.AndroidUtil;
import com.mtouche.android.juzfrens.util.Constants;
import com.mtouche.android.juzfrens.util.NotificationManagerClass;
import com.mtouche.android.juzfrens.util.User;

public class ContactList extends ExpandableListActivity {

private String className = this.getClass().getSimpleName();
private ExpandableListAdapter mAdapter;
//private int savegroup;
private AlertDialog.Builder alertDialog;

/*private Message objMsg;
private Bundle objBundle;
private Handler handler;
*/
private static final int ADD_FRIENDS = Menu.FIRST;
private static final int MYPROFILE = Menu.FIRST + 1;
private static final int SIGNOUT = Menu.FIRST + 2;

private static final int ACTIVITY_CREATE = 0;
//private static final int ACTIVITY_EDIT = 1;

private static final int SEND_SMS = 0;
private static final int SEND_OFFLINE = 1;
private static final int CANCEL = 2;

private Integer smsCost;

public ContactList() {

String methodName = "Constructor ContactList";
Log.d(className, methodName + "[Begin]");
if(Engine.flag) {

ContactsTimerTask contactsTimerTask = new ContactsTimerTask();
Engine.contactsTimer.schedule(contactsTimerTask, 0, 3000);
Engine.flag = false;
}
Log.d(className, methodName + "[End]");
}

@Override
public void onCreate(Bundle savedInstanceState) {

String methodName = "onCreate";
super.onCreate(savedInstanceState);
Log.d(className, methodName + " [Begin]");

Engine.vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
Engine.context = ContactList.this;

alertDialog = new AlertDialog.Builder(this);

NotificationManagerClass.notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
NotificationManagerClass.notificationIntent = new Intent(android.content.Intent.ACTION_VIEW, null);
NotificationManagerClass.pendingIntent = PendingIntent.getActivity(ContactList.this, 0, NotificationManagerClass.notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
getExpandableListView().setDivider(null);
getExpandableListView().setDividerHeight(0);
getExpandableListView().expandGroup(0);
getExpandableListView().expandGroup(1);
getExpandableListView().expandGroup(2);

Log.d(className, methodName + " Engine.friends [" + Engine.friends + "]");

getExpandableListView().setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
String methodName = "onChildClick";

Log.d(className, methodName + " [Begin]");

Engine.nickName = (String) expandableListView.getExpandableListAdapter().getChild(groupPosition, childPosition);
//Log.d(className, methodName + " groupPosition[" + groupPosition + "]childPosition[" + childPosition + "]");
//Log.d(className, methodName + " selectedChild[" + selectedChild + "]");

if(groupPosition == 0) {
Intent intent = new Intent(ContactList.this, Conversation.class);
ContactList.this.startActivityForResult(intent, ACTIVITY_CREATE);
//ContactList.this.startActivity(objIntent);
//ContactList.this.finish();
} else
if(groupPosition == 1) {
Intent intent = new Intent(ContactList.this, Chat.class);
ContactList.this.startActivityForResult(intent, ACTIVITY_CREATE);

//ContactList.this.startActivity(objIntent);
//ContactList.this.finish();
} else
if(groupPosition == 2) {

/* objMsg = new Message();
objBundle = new Bundle();
handler = new Handler();*/

AlertDialog.Builder alertDialog = new AlertDialog.Builder(ContactList.this);
alertDialog.setTitle(getString(R.string.sendMessage));
alertDialog.setItems(R.array.popup, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int selectedOption) {

String methodName = "onClick";
Log.d(className, methodName + " selectedOption :[" + selectedOption + "]");
Intent intent;
switch(selectedOption) {
case SEND_SMS: // Send As SMS
/*objBundle.putBoolean("ISSMSOFFLINE", true);
objMsg.setData(objBundle);
handler.sendMessage(objMsg);*/
HashMap<String, Integer> telcoIdsAndSMSCosts = (HashMap<String, Integer>)Engine.configuration.get(Constants.SMS_CREDIT);
smsCost = telcoIdsAndSMSCosts.get(Engine.senderTelcoId.toString());
if(smsCost > Engine.credits) {
showMessage(getString(R.string.inSufficientCredits));
Log.d(className, methodName + " smsCost[" + smsCost + "]");
Log.d(className, methodName + " Engine.credits[" + Engine.credits + "]");
} else {
Engine.isSMSOffline = true;
intent = new Intent(ContactList.this, OfflineSMS.class);
ContactList.this.startActivityForResult(intent, ACTIVITY_CREATE);
}
//ContactList.this.startActivity(objIntent);
//ContactList.this.finish();
break;
case SEND_OFFLINE: // Send As Offline Message
/*objBundle.putBoolean("ISSMSOFFLINE", false);
objMsg.setData(objBundle);
handler.sendMessage(objMsg);*/
Engine.isSMSOffline = false;
intent = new Intent(ContactList.this, OfflineSMS.class);
ContactList.this.startActivityForResult(intent, ACTIVITY_CREATE);
//ContactList.this.startActivity(objIntent);
//ContactList.this.finish();
break;
case CANCEL: //Cancel
break;
}
}
});
alertDialog.create();
alertDialog.show();
}
Log.d(className, methodName + " [End]");
return false;
}
});

getExpandableListView().setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
// TODO Auto-generated method stub
String methodName = "onKey";
Log.d(className, methodName + " [Begin]");
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d(className, methodName + " keyCode [" + keyCode + "]");
return true;
}
Log.d(className, methodName + " [End]");
return false;
}
});

Log.d(className, methodName + " [End]");
}


@Override
public boolean onContextItemSelected(MenuItem menuItem) {

ExpandableListContextMenuInfo expandableListContextMenuInfo = (ExpandableListContextMenuInfo) menuItem.getMenuInfo();
if(menuItem.getTitle().equals(Constants.CANCEL)) {
return false;
}
int groupPos = ExpandableListView.getPackedPositionGroup(expandableListContextMenuInfo.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(expandableListContextMenuInfo.packedPosition);
Engine.nickName = Engine.nickNames[groupPos][childPos];

return true;
}

/**
* A simple adapter which maintains an ArrayList of photo resource Ids. Each
* photo is displayed as an image. This adapter supports clearing the list
* of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

// Sample data set. children[i] contains the children (String[]) for
// groups[i].

public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 55);

TextView textView = new TextView(ContactList.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 4, 0, 0);
return textView;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

String methodName = "getChildView";
Log.d(className, methodName + " [Begin]");
ContactListView objContact;
//Context context = getApplicationContext();
// ImageView imageView = new ImageView(context);
if (groupPosition != 2) {
if (convertView == null)
objContact = new ContactListView(getApplicationContext(), true, Engine.nickNames[groupPosition][childPosition]);
else
objContact = (ContactListView) convertView;

objContact.setNickname(Engine.nickNames[groupPosition][childPosition]);
//User user = Engine.onlineFriends.get(Engine.nickNames[groupPosition][childPosition]);
User user = Engine.friends.get(Engine.nickNames[groupPosition][childPosition]);
int imageIcon = AndroidUtil.getImageIcon(user);
objContact.setStatusIcon(getResources().getDrawable(imageIcon));
String nickName = Engine.nickNames[groupPosition][childPosition];
Bitmap bitmap = Engine.friends.get(nickName).getBitmapImage();
objContact.setAvatar(bitmap);
return objContact;
} else {
if (convertView == null)
objContact = new ContactListView(getApplicationContext(), true, Engine.nickNames[groupPosition][childPosition]);
else
objContact = (ContactListView) convertView;

objContact.setNickname(Engine.nickNames[groupPosition][childPosition]);
objContact.setStatusIcon(getResources().getDrawable(R.drawable.img_offline));
String nickName = Engine.nickNames[groupPosition][childPosition];
Bitmap bitmap = Engine.friends.get(nickName).getBitmapImage();
objContact.setAvatar(bitmap);
Log.d(className, methodName + " [End]");
return objContact;
}
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
Typeface typeface = Typeface.create("MS Sans Serif", Typeface.BOLD);
textView.setTypeface(typeface);
textView.setText(getGroup(groupPosition).toString());
//savegroup = groupPosition;
textView.post(null);
return textView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
//String methodName = "isChildSelectable";
//String selectedChild = (String) getChild(groupPosition, childPosition);
return true;
}

public boolean isGroupSelectable(int groupPosition) {
//String methodName = "isGroupSelectable";
//Log.d(className, methodName + " groupPosition[" + groupPosition + "]");
return true;
}

public boolean hasStableIds() {
//String methodName = "hasStableIds";
//Log.d(className, methodName);
return true;
}

public Object getChild(int groupPosition, int childPosition) {
return Engine.nickNames[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

public int getChildrenCount(int groupPosition) {
return Engine.nickNames[groupPosition].length;
}

public Object getGroup(int groupPosition) {
return Engine.groups[groupPosition];
}

public int getGroupCount() {
return Engine.groups.length;
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADD_FRIENDS, 1, R.string.addFriends);
menu.add(0, MYPROFILE, 2, R.string.myProfile);
menu.add(0, SIGNOUT, 3, R.string.signOut);
return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {

case ADD_FRIENDS : addFriends();
break;
case MYPROFILE : myProfile();
break;
case SIGNOUT : signOut();
//return true;
}
return super.onMenuItemSelected(featureId, item);
}

public void addFriends() {
String methodName = "addFriends";
Log.d(className, methodName + "[Begin]");
Log.d(className, methodName + "[End]");
}

public void myProfile() {
String methodName = "myProfile";
Log.d(className, methodName + "[Begin]");
Log.d(className, methodName + "[End]");
}

public void signOut() {
String methodName = "signOut";
Log.d(className, methodName + "[Begin]");
Engine.objXMLRPCHelper.doSignOut();
Intent objIntent = new Intent(ContactList.this, SignIn.class);
ContactList.this.startActivity(objIntent);
ContactList.this.finish();
//finish();
Log.d(className, methodName + "[End]");
}

public synchronized void loadOnlineOfflineFriends() {
String methodName = "loadOnlineOfflineFriends";
Log.d(className, methodName + " [Begin]");
Engine.objXMLRPCHelper.doGetFriends();
Engine.objXMLRPCHelper.doGetOnlineFriendsList();
String [][]nickNames = AndroidUtil.getNickNames(Engine.p_objFriends, Engine.p_objOnlineFriendsList);
Engine.nickNames = nickNames;
AndroidUtil.conversationContacts(Engine.receivedMessages);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
String methodName = "onActivityResult";
Log.d(className, methodName + " [Begin]");
//Log.d(className, methodName + " requestCode [" + requestCode + "]");
//Log.d(className, methodName + " resultCode [" + resultCode + "]");
super.onActivityResult(requestCode, resultCode, intent);
Log.d(className, methodName + " [End]");
}

public void showMessage(String message) {

alertDialog.setTitle(Constants.MESSAGE_TEXT);
alertDialog.setMessage(message);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
}
});
alertDialog.show();
}

class ContactsTimerTask extends TimerTask {

private String className = this.getClass().getSimpleName();
public void run() {
String methodName = "run";
Log.d(className, methodName + " [Begin]");
loadOnlineOfflineFriends();
Log.d(className, methodName + " [End]");
}
}

}

******************************************************************************************************************************************************************


Thanks
Srinu
Attachments
juzfrens.jpg
juzfrens.jpg (33.02 KiB) Viewed 1784 times
srinuv28
Developer
Developer
 
Posts: 36
Joined: Wed Nov 04, 2009 4:04 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests