0

I have this weird bug that I cannot seem to diagnose, I have 2 fields one is getting the value from the DB and the other one is not.

My problem is that "friends[position].userPresence" isn't returning any values from DB, but an Identical one called "friends[position].userStatus" is returning values, but when I assign userPresence's column name to userStatus, then userStatus is returning the correct info for the column.

Any help would be highly appreciated!

public class FriendList extends ListActivity {
private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
private static final int EXIT_APP_ID = Menu.FIRST + 1;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;

public String ownusername = new String();

private class FriendListAdapter extends BaseAdapter {
    class ViewHolder {
        TextView text;
        TextView status;
        ImageView avatar;
        ImageView onlstatus;
    }

    private LayoutInflater mInflater;
    private Bitmap mOnlineIcon;
    private Bitmap mOfflineIcon;
    private Bitmap mAwayIcon;
    private Bitmap mPersonPic;

    private FriendInfo[] friends = null;

    public FriendListAdapter(Context context) {
        super();

        mInflater = LayoutInflater.from(context);

        mOnlineIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.online);
        mOfflineIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.offline);
        mAwayIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.away);
        mPersonPic = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.no_photo_icon_big);

    }

    public void setFriendList(FriendInfo[] friends) {
        this.friends = friends;
    }

    @Override
    public int getCount() {

        return friends.length;
    }

    @Override
    public FriendInfo getItem(int position) {

        return friends[position];
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid
        // unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is
        // no need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.friend_list_screen,
                    null);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.userName);
            holder.status = (TextView) convertView
                    .findViewById(R.id.userStatusMsg);
            holder.onlstatus = (ImageView) convertView
                    .findViewById(R.id.icon_status);
            holder.avatar = (ImageView) convertView.findViewById(R.id.icon);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.text.setText(friends[position].userName);
        holder.onlstatus
                .setImageBitmap(friends[position].userStatus == "Available" ? mOnlineIcon :
                        friends[position].userStatus == "Busy" ? mOfflineIcon : mAwayIcon);
        holder.avatar.setImageBitmap(mPersonPic);
        holder.status.setText(friends[position].userPresence);
        /*
         * holder.icon .setImageBitmap(friends[position].status ==
         * STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
         */

        return convertView;
    }

}

FriendInfo

public class FriendList extends ListActivity {
private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;
private static final int EXIT_APP_ID = Menu.FIRST + 1;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;

public String ownusername = new String();

private class FriendListAdapter extends BaseAdapter {
    class ViewHolder {
        TextView text;
        TextView status;
        ImageView avatar;
        ImageView onlstatus;
    }

    private LayoutInflater mInflater;
    private Bitmap mOnlineIcon;
    private Bitmap mOfflineIcon;
    private Bitmap mAwayIcon;
    private Bitmap mPersonPic;

    private FriendInfo[] friends = null;

    public FriendListAdapter(Context context) {
        super();

        mInflater = LayoutInflater.from(context);

        mOnlineIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.online);
        mOfflineIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.offline);
        mAwayIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.away);
        mPersonPic = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.no_photo_icon_big);

    }

    public void setFriendList(FriendInfo[] friends) {
        this.friends = friends;
    }

    @Override
    public int getCount() {

        return friends.length;
    }

    @Override
    public FriendInfo getItem(int position) {

        return friends[position];
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid
        // unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is
        // no need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.friend_list_screen,
                    null);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.userName);
            holder.status = (TextView) convertView
                    .findViewById(R.id.userStatusMsg);
            holder.onlstatus = (ImageView) convertView
                    .findViewById(R.id.icon_status);
            holder.avatar = (ImageView) convertView.findViewById(R.id.icon);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.text.setText(friends[position].userName);
        holder.onlstatus
                .setImageBitmap(friends[position].userStatus == "Available" ? mOnlineIcon :
                        friends[position].userStatus == "Busy" ? mOfflineIcon : mAwayIcon);
        holder.avatar.setImageBitmap(mPersonPic);
        holder.status.setText(friends[position].userPresence);
        /*
         * holder.icon .setImageBitmap(friends[position].status ==
         * STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
         */

        return convertView;
    }

}

public class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("Broadcast receiver ", "received a message");
        Bundle extra = intent.getExtras();
        if (extra != null) {
            String action = intent.getAction();
            if (action.equals(IMService.FRIEND_LIST_UPDATED)) {
                // taking friend List from broadcast
                // String rawFriendList =
                // extra.getString(FriendInfo.FRIEND_LIST);
                // FriendList.this.parseFriendInfo(rawFriendList);
                FriendList.this.updateData(
                        FriendController.getFriendsInfo(),
                        FriendController.getUnapprovedFriendsInfo());

            }
        }
    }

};

public MessageReceiver messageReceiver = new MessageReceiver();

private ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        imService = ((IMService.IMBinder) service).getService();

        FriendInfo[] friends = FriendController.getFriendsInfo(); // imService.getLastRawFriendList();
        if (friends != null) {
            FriendList.this.updateData(friends, null); // parseFriendInfo(friendList);
        }

        // setTitle(imService.getUsername() + "'s friend list");
        CommonUtility.setCustomTitlebar(FriendList.this, imService);
        ownusername = imService.getUsername();
        ((LinearLayout)findViewById(R.id.actionbar_layout)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(FriendList.this,UserProfile.class));
            }
        });
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
        imService = null;
        Toast.makeText(FriendList.this, R.string.local_service_stopped,
                Toast.LENGTH_SHORT).show();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.list_screen);
    friendAdapter = new FriendListAdapter(this);
}

public void updateData(FriendInfo[] friends, FriendInfo[] unApprovedFriends) {
    if (friends != null) {
        friendAdapter.setFriendList(friends);
        setListAdapter(friendAdapter);
    }

    if (unApprovedFriends != null) {
        NotificationManager NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (unApprovedFriends.length > 0) {
            String tmp = new String();
            for (int j = 0; j < unApprovedFriends.length; j++) {
                tmp = tmp.concat(unApprovedFriends[j].userName).concat(",");
            }
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    this).setSmallIcon(R.drawable.ic_launcher) // TODO Redo
                                                                // friend
                                                                // notification
                    .setContentTitle(
                            getText(R.string.new_friend_request_exist));
            /*
             * Notification notification = new
             * Notification(R.drawable.stat_sample,
             * getText(R.string.new_friend_request_exist),
             * System.currentTimeMillis());
             */

            Intent i = new Intent(this, UnApprovedFriendList.class);
            i.putExtra(FriendInfo.FRIEND_LIST, tmp);

            PendingIntent contentIntent = PendingIntent.getActivity(this,
                    0, i, 0);

            mBuilder.setContentText("You have new friend request(s)");
            /*
             * notification.setLatestEventInfo(this,
             * getText(R.string.new_friend_request_exist),
             * "You have new friend request(s)", contentIntent);
             */

            mBuilder.setContentIntent(contentIntent);

            NM.notify(R.string.new_friend_request_exist, mBuilder.build());
        } else {
            // if any request exists, then cancel it
            NM.cancel(R.string.new_friend_request_exist);
        }
    }

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);

    Intent i = new Intent(this, Messaging.class);
    FriendInfo friend = friendAdapter.getItem(position);
    i.putExtra(FriendInfo.USERNAME, friend.userName);
    i.putExtra(FriendInfo.PORT, friend.port);
    i.putExtra(FriendInfo.IP, friend.ip);
    startActivity(i);
}

@Override
protected void onPause() {
    unregisterReceiver(messageReceiver);
    unbindService(mConnection);
    super.onPause();
}

@Override
protected void onResume() {

    super.onResume();
    bindService(new Intent(FriendList.this, IMService.class), mConnection,
            Context.BIND_AUTO_CREATE);

    IntentFilter i = new IntentFilter();
    // i.addAction(IMService.TAKE_MESSAGE);
    i.addAction(IMService.FRIEND_LIST_UPDATED);

    registerReceiver(messageReceiver, i);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    boolean result = super.onCreateOptionsMenu(menu);

    menu.add(0, ADD_NEW_FRIEND_ID, 0, R.string.add_new_friend);

    menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);

    return result;
}

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

    switch (item.getItemId()) {
    case ADD_NEW_FRIEND_ID: {
        Intent i = new Intent(FriendList.this, AddFriend.class);
        startActivity(i);
        return true;
    }
    case EXIT_APP_ID: {
        imService.exit();
        finish();
        return true;
    }
    }

    return super.onMenuItemSelected(featureId, item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

}
}

The XML Handler

    public void startElement(String uri, String localName, String name,
        Attributes attributes) throws SAXException {
    if (localName == "friend") {
        FriendInfo friend = new FriendInfo();
        friend.userName = attributes.getValue(FriendInfo.USERNAME);
        String status = attributes.getValue(FriendInfo.STATUS);
        friend.ip = attributes.getValue(FriendInfo.IP);
        friend.port = attributes.getValue(FriendInfo.PORT);
        friend.userPresence = attributes.getValue(FriendInfo.USER_PRESENCE);
        friend.userStatus = attributes.getValue(FriendInfo.USER_STATUS);
        friend.userDisplayName = attributes.getValue(FriendInfo.USER_DISPNAME);
        friend.userKey = attributes.getValue(FriendInfo.USER_KEY);
        friend.expire = attributes.getValue("expire");

        if (status != null && status.equals("online")) {
            friend.status = STATUS.ONLINE;
            mOnlineFriends.add(friend);
        } else if (status.equals("unApproved")) {
            friend.status = STATUS.UNAPPROVED;
            mUnapprovedFriends.add(friend);
        } else {
            friend.status = STATUS.OFFLINE;
            mFriends.add(friend);
        }
    } else if (localName == "user") {
        this.userKey = attributes.getValue(FriendInfo.USER_KEY);
    } else if (localName == "message") {
        MessageInfo message = new MessageInfo();
        message.userid = attributes.getValue(MessageInfo.USERID);
        message.sendt = attributes.getValue(MessageInfo.SENDT);
        message.messagetext = attributes.getValue(MessageInfo.MESSAGETEXT);
        Log.i("MessageLOG", message.userid + message.sendt
                + message.messagetext);
        mUnreadMessages.add(message);
    }
    super.startElement(uri, localName, name, attributes);
}

FriendInfo Class public class FriendInfo {

public static final String FRIEND_LIST = "friendList";
public static final String USERNAME = "username";
public static final String IP = "IP";
public static final String PORT = "port";
public static final String USER_KEY = "userKey";
public static final String USER_DISPNAME = "displayname";
public static final String MESSAGE = "message"; // this should not be in
                                                // here
public static final String STATUS = "status";
public static final String USER_STATUS = "status_msg";
public static final String USER_PRESENCE = "status_msg2";

public STATUS status;
public String userStatus;
public String userPresence;
public String userDisplayName;
public String userName;
public String ip;
public String port;
public String userKey;
public String expire;

};

Shaun
  • 559
  • 1
  • 3
  • 17
  • Are you sure you're showing the right code? Where is the code that loads stuff from the DB? Is your DB XML? Where is the code that saves userStatus and userPresence? And where is the class FriendInfo? – gnobal Oct 31 '13 at 11:23
  • Sorry. I added the frinedinfo class. The DB is mysql but there is a string that passes to the server and returns as XML – Shaun Oct 31 '13 at 11:45
  • 1
    That's better, but I still can't find anywhere where things are being saved. Another thing in your code is that you're using `==` to compare strings, which I believe is wrong (and maybe related to your issue). You should use `equals()` to compare strings ( http://stackoverflow.com/questions/767372/java-string-equals-versus ) – gnobal Oct 31 '13 at 11:50

1 Answers1

0

I don't see what the solution is. However, I would suggest you flood your program with System.out.println() statements to see what values are throughout your program. Alternately (and better) is to use your IDE's debugger and set breakpoints to examine values.

user2810910
  • 279
  • 1
  • 2
  • Thanks. This wasn't the solution but helped me find the solution, I forgot to add the specific field in the XML "MYsql" query on server side, so it was never pulling it and the code was getting from an unknown field – Shaun Nov 04 '13 at 11:27