如何在android中显示上一个活动中的下一个活动字符串

时间:2015-02-06 11:07:02

标签: android android-activity

您好,在下面我根据用户登录此应用程序显示使用listview图像在线或离线的朋友列表。 现在,以同样的方式我想显示groupname.But使用菜单我正在创建新组,即下一个活动。 但是组名我想在这里显示friendlist.java类,我创建它的组应该显示组名,以及谁是用户的朋友。

好友列表

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

    public String ownusername = new String();

    private class FriendListAdapter extends BaseAdapter 
    {       
        class ViewHolder {
            TextView text,text1;

            ImageView icon;
        }
        private LayoutInflater mInflater;
        private Bitmap mOnlineIcon;
        private Bitmap mOfflineIcon;        

        private FriendInfo[] friends = null;


        public FriendListAdapter(Context context) {
            super();            

            mInflater = LayoutInflater.from(context);

            mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
            mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

        }

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


        public int getCount() {     

            return friends.length;
        }


        public FriendInfo getItem(int position) {           

            return friends[position];
        }

        public long getItemId(int position) {

            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;


            if (convertView == null) 
            {
                convertView = mInflater.inflate(R.layout.friend_list_screen, null);


                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.text1 = (TextView) convertView.findViewById(R.id.text1);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);                                       

                convertView.setTag(holder);
            }   
            else {

                holder = (ViewHolder) convertView.getTag();
            }


            holder.text.setText(friends[position].userName);
            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))
                {

                    FriendList.this.updateData(FriendController.getFriendsInfo(), 
                                                FriendController.getUnapprovedFriendsInfo());

                }
            }
        }

    };
    public MessageReceiver messageReceiver = new MessageReceiver();

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

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

            setTitle(imService.getUsername() + "'s friend list");
            ownusername = imService.getUsername();
        }
        public void onServiceDisconnected(ComponentName className) {          
            imService = null;
            Toast.makeText(FriendList.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };



    protected void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);

        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.stat_sample)
                .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)");


                mBuilder.setContentIntent(contentIntent);


                NM.notify(R.string.new_friend_request_exist, mBuilder.build());         
            }
            else
            {

                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.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, CREATE_GROUP_ID, 0, R.string.create_group);
        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 CREATE_GROUP_ID:
            {
                Intent i = new Intent(FriendList.this, CreateGroup.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);




    }

下一个活动它将如何工作意味着首先要求输入群组名称然后接下来它将显示所有朋友,我可以选择n个朋友,然后选择一个名为创建的按钮。

现在已经与n个朋友成功创建了组,这些我将保存到数据库中的所有数据。 现在我的问题是在创建组后我想在之前的活动中显示组详细信息。

任何人都可以帮助我摆脱这个问题

2 个答案:

答案 0 :(得分:1)

在添加新组后导航回上一个活动时,您必须刷新列表数据。从第一个活动的onResume中的数据库中检索数据。

答案 1 :(得分:0)

我同意上面的答案,除了你需要调用NotifyDataChanged适配器视图的方法,它将使用提供的新数据重绘列表内容