根据服务器响应创建多个处理程序并管理那里的状态 [英] Create multiple handler based on server response and manage there states

查看:61
本文介绍了根据服务器响应创建多个处理程序并管理那里的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序收到服务器的响应时,我想创建多个处理程序并且可运行.

I want to create multiple handler and run-able when my app receive response from server.

处理程序可以最多4个,最小1个.

问题

实际上,我想将单元屏幕划分为不同的部分,划分之后,我需要在屏幕的所有部分中显示不同类型的数据.在这些部分中,每个部分都有多个要显示的项目.

Actually i want to divide cell screen in different part and after dividing i need to display different type of data in all parts of the screen. And in these parts, Each part have multiple items to display.

例如!用户希望将屏幕分为两部分

e.g! User want to divide screen in two parts

第一部分包含视频,图像和视频.(3)第一部分中的项目

part one contains a video, image and again a video. (3) item in part one

第二部分包含图像和视频.(2)第二部分.

part two contain image and a video. (2) item in part two.

每个项目都需要显示特定时间段(用户定义)

each item needs to display for a specific time period (user defined)

当一项完成的时间到时,应用程序需要在列表中显示下一项.

when the time for one item complete then app needs to display next item in list.

我尝试过的事情

据我所知,有一种方法可以实现这一目标

As per my knowledge there is one way to achieve this is that

我需要创建多个处理程序来处理所有部分.

I need to to create multiple handlers which will take care of all the parts.

我试图使用for循环创建新的处理程序并保存它的实例,但是这样做无法管理何时以及哪个屏幕部件需要更新.

i tried to create new handler using a for loop and save it instance but by doing so i am not able to manage that when and which screen part needs to be updated.

请任何人告诉我如何动态创建任意数量的处理程序.并保存状态.

Please can anyone tell me how to create any number of handlers dynamically. and save there states.

private void createStreamHandler(){

        for(int i=0; i < serverResponse2.vMedialist.size(); i++){
            final int index = i;
            Handler hadler = new Handler(); 
            hadler.postDelayed(myRunnable = new Runnable() {

                public void run() {

                    if (sDuration[0] <= 0){
                    // check if wait counter expired? get show next media and gets
                    // its display duration
                        sDuration[0] = runPlaylist(serverResponse2.vMedialist.get(index), index);
                    }

                    sDuration[0] -= 5;
                    handler.postDelayed(myRunnable, 5000);
                }
            }, 500);

            streamsHandler.add(hadler);
            streamRunnable.add(myRunnable);
        }
}



private int runPlaylist(ArrayList<Media2> mediaList, int index){

        Log.e("mediaList ", "mediaList size at " + index +" = " + mediaList.size());
        return mediaList.get(index).duration;
    }

每次我获取arraylist的最后一个索引的大小.并且只有最后一个处理程序继续运行.请帮助我坚持下去.

Every time i am getting the size of the last index of the arraylist. and only last handler keep on running. Please Help i am stuck at it.

我如何跟踪所有处理程序?

还有其他方法可以处理管理程序吗?

Is there any other way to do manage handlers.

我希望我已经很好地解释了这个问题.请提出任何解决方案.谢谢.

I hope i have explained the problem well. Please suggest any solution. Thanks.

推荐答案

如果我正确理解了您的代码,则需要替换

If I correctly understand your code you need to replace

handler.postDelayed(myRunnable, 5000); 

使用

handler.postDelayed(this, 5000); 

因为 myRunnable 是您创建的最新Runnable.然后您的所有处理程序都将其发布,而不是自己发布.

because myRunnable is the latest Runnable that you created. And all your handlers post it instead of themselfs.


您在每个处理程序中存储它的编号- index .您可以添加方法

void handleResponse(int index, Object someData)

在您的课程中,

创建处理程序.之后,每个处理程序都可以调用 handleResponse(index,data)通知某些更改.

to your class, that creates handlers. After that each handler can call handleResponse(index, data) to notify about some changes.

这篇关于根据服务器响应创建多个处理程序并管理那里的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆