求解,数组内容如何传送至listview内

来源:互联网 发布:免费域名提供商 编辑:程序博客网 时间:2024/04/30 13:47
跪求大神们帮解或提点。

目前手边有一只自己照网络模板改成的code,目的是要让使用者可以自行用拖曳的方式排序想要的顺序,再用socket的方式把这个顺序传送出去。

原先要送的信息是固定死的,总共需排序十列(十种颜色),所以我写成这样:

DragListActivity

public static final String _MPR_ = "MPR";
    public static final String _Yellow_ = "Y";
    public static final String _Green_ = "G";
    public static final String _Blue_ = "B";
    public static final String _DarkBlue_ = "DB";
    public static final String _Purple_ = "P";
    public static final String _Orange_ = "O";
    public static final String _MidnightBlue_ = "MB";
    public static final String _Pink_ ="PINK";
    public static final String _White_ ="W";

    private static final String[] LED ={ null, _MPR_, _Yellow_, _Green_, _Blue_, _DarkBlue_,
            _Purple_, _Orange_, _MidnightBlue_, _Pink_, _White_};


@Override
        public View getView(final int position, final View convertView, ViewGroup parent) {
            View view = convertView;

            if(groupKey.contains(getItem(position)) ){
                view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_tag, null);
            }else{
                switch (""+getItem(position))
                {
                    case "MPR":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_red, null);
                        break;
                    case "Y":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_yellow, null);
                        break;
                    case "G":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_green, null);
                        break;
                    case "B":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_blue, null);
                        break;
                    case "DB":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_darkblue, null);
                        break;
                    case "P":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_purple, null);
                        break;
                    case "O":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_orange, null);
                        break;
                    case "MB":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_midnightblue, null);
                        break;
                    case "PINK":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_pink, null);
                        break;
                    case "W":
                        view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_white, null);
                        break;
                }
            }

            TextView textView = (TextView)view.findViewById(R.id.drag_list_item_text);
            textView.setText(getItem(position));
            return view;
        }
    }


DragListView
每次拖曳颜色后,会先把顺序存好,之后,再把存的内容送出。

public void onDrop(int y){

        int tempPosition = pointToPosition(0, y);
        if(tempPosition != INVALID_POSITION){
            dragPosition = tempPosition;
        }

        if(y < getChildAt(1).getTop()){
            dragPosition = 1;
        }else if(y > getChildAt(getChildCount()-1).getBottom()){
            dragPosition = getAdapter().getCount()-1;
        }

        if(dragPosition > 1 && dragPosition < getAdapter().getCount()){
            @SuppressWarnings("unchecked")
            ActivityLightSort.DragListAdapter adapter = (ActivityLightSort.DragListAdapter)getAdapter();
            String dragItem = adapter.getItem(dragSrcPosition);
            adapter.remove(dragItem);
            adapter.insert(dragItem, dragPosition);
            pr_cmd = adapter.getList().toString();
            cmd = pr_cmd.substring(3,38).replaceAll(", ","");
            SaveSettingFile(cmd);
        }
    }


以上,可达成user拖曳颜色后,内容也一起变动,但现在内容却改成,会先從socket收到讯息,内容格式可能是这样:RsrR@B=1=050505=1000000000=2015=02=030303=02020A

RsrR@B=1=050505=1000000000=2015=,几乎是固定死的信息,会变动的只有1000000000,指时间戳
02表示后面有多少机器mac的数量,这表示有2个
030303 第一个机器的mac
02020A 第二个机器的mac

我的想法想先把收到的讯息处理,获取出后面的mac来,再把mac填入listview内。

例如把030303和02020A填入LED[1]和LED[2]内(LED[0]固定为null),所以就先利用 「=」来分割出内容:

String memberlist = SharePreferenceUtil.get_MemberList(ActivityLightSort.this);
        System.out.println("lightsort收到的資訊:" + memberlist);

        AfterSplit = memberlist.split("=");
        for (int i = 0; i < AfterSplit.length; i++)
        {
            System.out.println(AfterSplit[i]);
        }


但是AfterSplit数组的内容不知要怎么放到LED数组内,跪求了

最终的目的是要把收到的mac放入listview,LED[0]不允许拖曳,所以内容设为null,收到的mac则依序放入LED[1]、LED[2]、LED[3]……最多只会显示十列。

写法是参考谦虚的天下博主的文章改写的。
http://www.cnblogs.com/qianxudetianxia/archive/2011/06/12/2068761.html 

之前是碰到拖曳时,背景色不会一起交换,有在版上寻问过,此有图
http://bbs.csdn.net/topics/391913340
0 0
原创粉丝点击