有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java值不从字段更新,但相同的字段会更新

我有一个奇怪的错误,我似乎无法诊断,我有两个字段,一个是从数据库获取值,另一个不是

我的问题是,“friends[position].userPresence”没有从DB返回任何值,但一个名为“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类 公共类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;

}


共 (1) 个答案

  1. # 1 楼答案

    我不知道解决办法是什么。然而,我建议你在你的程序中加入系统。出来println()语句来查看整个程序中的值。或者(更好的)是使用IDE的调试器并设置断点来检查值