【问题标题】:Combine 3 string arraylists组合3个字符串arraylist
【发布时间】:2016-01-01 13:31:08
【问题描述】:

首先祝大家新年快乐,万事如意。

我有一个问题。假设我有 3 个字符串数组列表:

String group_names[] = { "Groupe A", "Groupe B", "Groupe C",
           "Groupe D", "Groupe E" };

String country_names[] = { "Brazil", "Mexico", "Croatia", "Cameroon",
           "Netherlands", "Chile", "Spain", "Australia", "Colombia" };

String country_values[] = { "Value1", "Value2", "Value3", "Value4",
           "Value5", "Value6", "Value7", "Value8", "Value9" };

有没有可能得到这样的输出?

提前谢谢你。

  • A组

  • 巴西 - Value1

  • 墨西哥 - Value2

  • 克罗地亚 - Value3

  • B组

  • 喀麦隆 - Value4

  • 荷兰 - Value5

  • C组

  • 智利 - Value6

  • 西班牙 - Value7

  • D组

  • 澳大利亚 - Value8

  • E组

  • 哥伦比亚 - Value9

已经尝试了for 循环,但它不适合我。

public ArrayList<Group> SetStandardGroups() {
    //.... 
    // The 3 arraylists
    ArrayList<Group> list = new ArrayList<Group>();
    ArrayList<Child> ch_list;
    int size = 5;
    int j = 0;
    for (String group_name : group_names) {
        Group gru = new Group();
        gru.setName(group_name);
        ch_list = new ArrayList<Child>();
        for (; j < size; j++) {
            for (String i : country_names) {
                Child ch = new Child();
                ch.setName(country_names[j]);
                ch.setValue(country_values[j]);
                ch_list.add(ch);
            }
        }
        gru.setItems(ch_list);
        list.add(gru);
        size = size + 1;
    }
    return list;
}

【问题讨论】:

  • 您的模型不包含有关国家/地区与组连接的任何信息。除此之外,j 永远不会设置为 0(或任何其他值),因此您在第一个组中对其进行迭代,然后所有组将为空,因为 for 循环的条件是 false .
  • 嗨 Gergely,我尝试创建一个自定义 ExpandableListView 如下:tutorialsbuzz.com/2014/07/… 我只是将 imageview 替换为第二个 Textview。

标签: android arraylist


【解决方案1】:

如果您只需要在子视图中并排显示两个textView(而不是图像和文本),则根本不需要循环遍历country_names

只需将您的getChildView() 更新为,

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) 
{

    Child child = (Child) getChild(groupPosition, childPosition);
    if (convertView == null) 
    {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);
    }
    TextView tv1 = (TextView) convertView.findViewById(R.id.country_name);
    TextView tv2 = (TextView) convertView.findViewById(R.id.value);

    tv1.setText(child.getName().toString());
    tv2.setText(child.getValue().toString());

    return convertView;
}

假设您的child_item.xml 类似于,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:layout_marginLeft="50dp">

    <TextView
        android:id="@+id/country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="20dp" />

      <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="20dp" />

</RelativeLayout>

希望对你有帮助。

编辑在您发表评论后,我认为您需要为每个组提供一组尺寸。说,

int group_sizes[] = { 3, 2, 2, 1, 1}; 

你的问题中的代码我会重写为,

public ArrayList<Group> SetStandardGroups() 
{
    //....
    // The 3 arraylists
    ArrayList<Group> list = new ArrayList<Group>();
    ArrayList<Child> ch_list;
    int size = 0, i = -1, j = 0;
    for (String group_name : group_names) 
    {
        i++;
        size = size + group_sizes[i];
        Group gru = new Group();
        gru.setName(group_name);
        ch_list = new ArrayList<Child>();
        for (; j < size; j++) 
        {
            Child ch = new Child();
            ch.setName(country_names[j]);
            ch.setValue(country_values[j]);
            ch_list.add(ch);
        }
        gru.setItems(ch_list);
        list.add(gru);
    }
    return list;
}

希望这能解决列出的所有问题。

【讨论】:

  • 嗨 Biju Parvathy,谢谢你的回答。确实,我有您所描述的 child_item.xml,但主要问题是如何正确设置值。 Groupe A 比 Groupe B 包含更多的值。你明白我的意思吗?
  • @Simon,是的,我注意到了。您的示例是使用固定大小 4 来执行所有操作。如果您需要每个组的大小可变,那么每个组的项目是否有任何特定的列表/数组[例如,{3,2,2,1,1}]?请确认,将编辑我的答案。谢谢。
  • 我的 3 个数组列表与问题中所述完全相同。你是这个意思吗?再次感谢您的帮助。
  • @Simon 你怎么知道这些项目之间的关系?您的模型是错误的,因为它不包含此基本信息。尝试使用不同的模型结构,如 JSON,然后使用本机 JSON solutions 或外部库(如 GSON)对其进行解析。
  • @Simon,希望最新的编辑对您有所帮助。不过,这是一个临时解决方案,如果您是出于项目目的而不是爱好试用,我会同意 Gergely。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2010-12-27
  • 1970-01-01
  • 2013-02-25
相关资源
最近更新 更多