I need a solution how to get the OnChildClick working for my
ExpanableListActivity.
when starting of this project the Listener was working, but now i get
nothing out of it?
i tried setting the listener through:
"getExpandableListView().setOnChildClickListener(this);"
at several point, but it isn't responding.
Here's the source code:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import
android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import de.hdmstuttgart.yaxim.IXMPPUiCallback.Stub;
import de.hdmstuttgart.yaxim.data.RosterItem;
import de.hdmstuttgart.yaxim.dialogs.AddRosterItemDialog;
import de.hdmstuttgart.yaxim.dialogs.ChangeStatusDialog;
import de.hdmstuttgart.yaxim.dialogs.MoveRosterItemToGroupDialog;
import de.hdmstuttgart.yaxim.dialogs.RemoveRosterItemDialog;
import de.hdmstuttgart.yaxim.dialogs.RenameRosterGroupDialog;
import de.hdmstuttgart.yaxim.dialogs.RenameRosterItemDialog;
import de.hdmstuttgart.yaxim.preferences.AccountPrefs;
import de.hdmstuttgart.yaxim.preferences.MainPrefs;
import de.hdmstuttgart.yaxim.service.XMPPService;
import de.hdmstuttgart.yaxim.util.AdapterConstants;
import de.hdmstuttgart.yaxim.util.ExpandableRosterAdapter;
public class MainWindow extends GenericExpandableListActivity {
private static final int CONNECT = Menu.FIRST + 1;
private static final int ADD_FRIEND = Menu.FIRST + 2;
private static final int SHOW_HIDE = Menu.FIRST + 3;
private static final int STATUS = Menu.FIRST + 4;
private static final int EXIT = Menu.FIRST + 5;
private static final int SETTINGS = Menu.FIRST + 6;
private static final int ACC_SET = Menu.FIRST + 7;
private static final int NOTIFY_ID = 0;
private static final String TAG = "MainWindow";
private static final int DIALOG_CONNECTING = 1;
private Intent xmppServiceIntent;
private ServiceConnection xmppServiceConnection;
private NotificationManager notificationMGR;
private XMPPServiceAdapter serviceAdapter;
private Stub uiCallback;
private List<ArrayList<HashMap<String, RosterItem>>> rosterEntryList;
private List<HashMap<String, String>> rosterGroupList;
private ExpandableRosterAdapter rosterListAdapter;
private Handler mainHandler;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainHandler = new Handler();
rosterEntryList = new ArrayList<ArrayList<HashMap<String,
RosterItem>>>();
rosterGroupList = new ArrayList<HashMap<String, String>>();
registerXMPPService();
createUICallback();
setContentView(R.layout.main);
setNotificationManager();
registerForContextMenu(getExpandableListView());
}
@Override
protected void onPause() {
super.onPause();
serviceAdapter.unregisterUICallback(uiCallback);
unbindXMPPService();
}
@Override
protected void onResume() {
super.onResume();
notificationMGR.cancel(NOTIFY_ID);
bindXMPPService();
}
private void createRosterIfConnected() {
if ((serviceAdapter != null)
&& (serviceAdapter.isServiceAuthenticated())) {
createRoster();
}
}
public void updateRoster() {
if (serviceAdapter.isAuthenticated()
&& getExpandableListAdapter() != null) {
rosterEntryList.clear();
createRosterEntryList();
rosterGroupList.clear();
createRosterGroupList();
rosterListAdapter.notifyDataSetChanged();
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
ExpandableListView.ExpandableListContextMenuInfo info;
try {
info =
(ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
Log.e(TAG, "bad menuinfo: ", e);
return;
}
long packedPosition = info.packedPosition;
boolean isChild = isChild(packedPosition);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.roster_menue, menu);
int groupPosition = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
String groupName =
rosterEntryList.get(groupPosition).get(0).get(
AdapterConstants.CONTACT_ID).getGroup();
if (isChild) {
menu.setGroupVisible(R.id.roster_contacts_group, true);
menu.setGroupVisible(R.id.roster_groups_group, false);
menu.setHeaderTitle(R.string.MenuContext_ContactHeaderTitle);
} else {
if (groupName.equals(AdapterConstants.EMPTY_GROUP)) {
menu.setGroupVisible(R.id.roster_contacts_group, false);
menu.setGroupVisible(R.id.roster_groups_group,
false);
menu.setHeaderTitle(R.string.MenuContext_HeaderTitleDisabled);
} else {
menu.setGroupVisible(R.id.roster_contacts_group, false);
menu.setGroupVisible(R.id.roster_groups_group,
true);
menu.setHeaderTitle(R.string.MenuContext_GroupsHeaderTitle);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return applyMenuContextChoice(item);
}
private boolean applyMenuContextChoice(MenuItem item) {
ExpandableListContextMenuInfo contextMenuInfo =
(ExpandableListContextMenuInfo) item
.getMenuInfo();
int groupPosition = ExpandableListView
.getPackedPositionGroup(contextMenuInfo.packedPosition);
int childPosition = ExpandableListView
.getPackedPositionChild(contextMenuInfo.packedPosition);
if (isChild(contextMenuInfo.packedPosition)) {
String user =
rosterEntryList.get(groupPosition).get(childPosition)
.get(AdapterConstants.CONTACT_ID).getJabberID();
int itemID = item.getItemId();
switch (itemID) {
case R.id.roster_openchat:
startChatActivity(user);
return true;
case R.id.roster_delete_contact:
RemoveRosterItemDialog deleteRosterItem = new
RemoveRosterItemDialog(
this, serviceAdapter, user);
deleteRosterItem.show();
return true;
case R.id.roster_rename_contact:
new RenameRosterItemDialog(this,
serviceAdapter, user).show();
return true;
case R.id.roster_editContactGroup:
new MoveRosterItemToGroupDialog(this,
serviceAdapter, user)
.show();
return true;
case R.id.roster_exit:
closeContextMenu();
return true;
}
} else {
int itemID = item.getItemId();
String seletedGroup =
rosterEntryList.get(groupPosition).get(0)
.get(AdapterConstants.CONTACT_ID).getGroup();
switch (itemID) {
case R.id.roster_rename_group:
new RenameRosterGroupDialog(this,
serviceAdapter, seletedGroup)
.show();
return true;
case R.id.roster_exit:
closeContextMenu();
return true;
}
}
return false;
}
private boolean isChild(long packedPosition) {
int type =
ExpandableListView.getPackedPositionType(packedPosition);
return (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD);
}
private void startChatActivity(String user) {
Intent chatIntent = new Intent(this,
de.hdmstuttgart.yaxim.chat.ChatWindow.class);
// set Uri for Intent to get Username
Uri userNameUri = Uri.parse(user);
chatIntent.setData(userNameUri);
startActivity(chatIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
populateMainMenu(menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return applyMainMenuChoice(item);
}
private void populateMainMenu(Menu menu) {
menu.add(Menu.NONE, CONNECT, Menu.NONE,
(getString(R.string.Menu_Connect))).setIcon(
R.drawable.yaxim_menu_connect);
menu.add(Menu.NONE, ADD_FRIEND, Menu.NONE,
(getString(R.string.Menu_addFriend))).setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, SHOW_HIDE, Menu.NONE,
(getString(R.string.Menu_ShowOff))).setIcon(
android.R.drawable.ic_menu_view);
menu.add(Menu.NONE, STATUS, Menu.NONE,
(getString(R.string.Menu_Status))).setIcon(
android.R.drawable.ic_menu_myplaces);
menu.add(Menu.NONE, EXIT, Menu.NONE, (getString
(R.string.Global_Exit)))
.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
menu.add(Menu.NONE, SETTINGS, Menu.NONE,
(getString(R.string.Menu_Settings))).setIcon(
android.R.drawable.ic_menu_preferences);
menu.add(Menu.NONE, ACC_SET, Menu.NONE,
(getString(R.string.Menu_AccSettings))).setIcon(
android.R.drawable.ic_menu_manage);
}
private boolean applyMainMenuChoice(MenuItem item) {
int itemID = item.getItemId();
switch (itemID) {
case CONNECT:
toggleConnection(item);
return true;
case ADD_FRIEND:
if (serviceAdapter.isServiceAuthenticated()) {
new AddRosterItemDialog(this,
serviceAdapter).show();
} else {
showToastNotification(R.string.Global_authenticate_first);
}
return true;
case SHOW_HIDE:
return true;
case STATUS:
if (serviceAdapter.isServiceAuthenticated()) {
new ChangeStatusDialog(this,
serviceAdapter).show();
} else {
showToastNotification(R.string.Global_authenticate_first);
}
return true;
case EXIT:
finish();
return true;
case SETTINGS:
startActivity(new Intent(this, MainPrefs.class));
return true;
case ACC_SET:
startActivity(new Intent(this, AccountPrefs.class));
return true;
}
return false;
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.i(TAG, "Called onChildClick()");
String user =
rosterEntryList.get(groupPosition).get(childPosition)
.get(AdapterConstants.CONTACT_ID).getJabberID();
startChatActivity(user);
return true;
}
private void toggleConnection(MenuItem item) {
if (serviceAdapter.isServiceAuthenticated()) {
serviceAdapter.disconnect();
stopService(xmppServiceIntent);
item.setIcon(R.drawable.yaxim_menu_connect);
item.setTitle(getString(R.string.Menu_connect));
clearRoster();
} else {
showDialog(DIALOG_CONNECTING);
startService(xmppServiceIntent);
item.setIcon(R.drawable.yaxim_menu_disconnect);
item.setTitle(getString(R.string.Menu_disconnect));
}
}
private void clearRoster() {
rosterEntryList.clear();
rosterGroupList.clear();
if (rosterListAdapter != null)
rosterListAdapter.notifyDataSetChanged();
}
private void registerXMPPService() {
Log.i(TAG, "called startXMPPService()");
xmppServiceIntent = new Intent(this, XMPPService.class);
xmppServiceIntent.setAction("de.hdmstuttgart.yaxim.XMPPSERVICE");
xmppServiceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name,
IBinder service)
{
Log.i(TAG, "called onServiceConnected()");
serviceAdapter = new
XMPPServiceAdapter(IXMPPService.Stub
.asInterface(service));
serviceAdapter.registerUICallback(uiCallback);
createRosterIfConnected();
}
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "called onServiceDisconnected()");
}
};
}
private void unbindXMPPService() {
try {
unbindService(xmppServiceConnection);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Service wasn't bound!");
}
}
private void bindXMPPService() {
bindService(xmppServiceIntent, xmppServiceConnection,
BIND_AUTO_CREATE);
}
private void setNotificationManager() {
notificationMGR = (NotificationManager) getSystemService
(NOTIFICATION_SERVICE);
}
private void registerListAdapter() {
createRosterEntryList();
createRosterGroupList();
rosterListAdapter = new ExpandableRosterAdapter(this,
rosterGroupList,
R.layout.maingroup_row,
AdapterConstants.GROUP_NAME,
new int[] { R.id.groupname }, rosterEntryList,
R.layout.mainchild_row,
AdapterConstants.CHILD_DATA_KEYS,
new int[] { R.id.roster_screenname,
R.id.roster_statusmsg });
setListAdapter(rosterListAdapter);
}
private void createRosterEntryList() {
List<String> rosterGroups = serviceAdapter.getRosterGroups();
for (String rosterGroup : rosterGroups) {
ArrayList<HashMap<String, RosterItem>> rosterGroupItems
=
getRosterGroupItems(rosterGroup);
rosterEntryList.add(rosterGroupItems);
}
}
private ArrayList<HashMap<String, RosterItem>> getRosterGroupItems(
String group) {
ArrayList<HashMap<String, RosterItem>> rosterItemList = new
ArrayList<HashMap<String, RosterItem>>();
List<RosterItem> rosterItems =
serviceAdapter.getGroupItems(group);
for (RosterItem rosterEntryItem : rosterItems) {
HashMap<String, RosterItem> rosterEntry = new
HashMap<String,
RosterItem>();
rosterEntry.put(AdapterConstants.CONTACT_ID,
rosterEntryItem);
rosterItemList.add(rosterEntry);
}
return rosterItemList;
}
private void createRosterGroupList() {
for (String rosterGroupName : serviceAdapter.getRosterGroups())
{
HashMap<String, String> tmpGroupMap = new
HashMap<String, String>
();
tmpGroupMap.put(AdapterConstants.GROUP_NAME[0],
rosterGroupName);
rosterGroupList.add(tmpGroupMap);
}
}
public void createRoster() {
Log.i(TAG, "called createRoster()");
if (serviceAdapter.isServiceAuthenticated()) {
clearRoster();
registerListAdapter();
expandGroups();
}
}
private void createUICallback() {
uiCallback = new IXMPPUiCallback.Stub() {
public void rosterChanged() throws RemoteException {
Log.i(TAG, "called rosterChanged()");
mainHandler.post(new Runnable() {
public void run() {
updateRoster();
}
});
}
public void connectionSuccessful() throws
RemoteException {
mainHandler.post(new Runnable() {
public void run() {
createRosterIfConnected();
if (progressDialog.isShowing())
dismissDialog(DIALOG_CONNECTING);
}
});
}
public void connectionFailed() throws RemoteException {
mainHandler.post(new Runnable() {
public void run() {
showToastNotification(R.string.toast_connectfail_message);
if (progressDialog.isShowing())
dismissDialog(DIALOG_CONNECTING);
}
});
}
};
}
public void expandGroups() {
for (int count = 0; count <
getExpandableListAdapter().getGroupCount
(); count++) {
getExpandableListView().expandGroup(count);
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_CONNECTING:
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(android.R.drawable.ic_dialog_info);
progressDialog.setTitle(R.string.dialog_connect_title);
progressDialog.setMessage(getText
(R.string.dialog_connect_message));
return progressDialog;
default:
return super.onCreateDialog(id);
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---