0

我有一个我似乎无法诊断的奇怪错误,我有 2 个字段,一个正在从数据库中获取值,另一个没有。

我的问题是“friends[position].userPresence”没有从数据库返回任何值,但是一个名为“friends[position].userStatus”的相同值正在返回值,但是当我将 userPresence 的列名分配给 userStatus,然后 userStatus正在返回该列的正确信息。

任何帮助将不胜感激!

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 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);

}
}

XML 处理程序

    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 类 public 类 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;

};

4

1 回答 1

0

我不明白解决方案是什么。但是,我建议您使用 System.out.println() 语句填充您的程序,以查看整个程序中的值。或者(更好)是使用 IDE 的调试器并设置断点来检查值。

于 2013-10-31T13:31:13.803 回答