0

我在android中有一个字符串。

str = |test1:23|st1:4|st2:3|st3:5|test2:25|st1:5|st2:6|st3:7|test3:26|st1:5|st2:2|st3:8|test4:28|st1:8|st2:3|st3:2|test5:29|st1:1|st2:2|st3:2|......

I need to split that string and set each data to listview in android.

I mean for each listitem I should keep the data as

test1:23
st1:4
st2:3
st3:5
---------------------
test2:25
st1:5
st2:6
st3:7
----------------------

现在我将用 | 分割字符串 符号使用:

 String splitstring = str.split("\\|");

现在splitstring[0]包含test1:23splitstring[1]包含sty:4。但我需要将前 4 个设置为第一个列表项,接下来的四个设置为下一个列表项,依此类推。请建议我如何完成该任务?提前致谢。

4

1 回答 1

1

怎么样:

    String str = "test1:23|st1:4|st2:3|st3:5|test2:25|st1:5|st2:6|st3:7|test3:26|st1:5|st2:2|st3:8|test4:28|st1:8|st2:3|st3:2|test5:29|st1:1|st2:2|st3:2";
    String[] splitted = str.split("\\|");
    List<String> fourItems = new ArrayList<String>();
    int listItemPosition = 0;
    for (String s : splitted) {
        fourItems.add(s);
        if (fourItems.size()==4) {
            processItems(fourItems, listItemPosition++);
            fourItems.clear();
        }
    }

    private void processItems(List<String> fourItems, int position) {
        // do whatever you want to do with your four strings like
        // assigning it to your list item at position
    }

当然,这是没有任何错误处理之类的

于 2013-06-19T03:02:47.370 回答