用于实现所需结果的数据结构是什么?

时间:2015-09-06 10:44:29

标签: java android data-structures spinner

我从服务器获取JSON响应,如下所示

{
    "ClassListTaughtByStaff": {
        "ClassTeacherTable": [
            {
                "StandardId": "96e671e2-c1fe-4879-b3bc-88c721292edf",
                "StandardName": "IV",
                "DivisionId": "ac2b6419-a01b-46e1-a5e3-23e84cb04af9",
                "DivisionName": "A"
            }
        ],
        "SubjectTeacherTable": [
            {
                "StandardId": "90da4426-1ba4-4f79-86a5-abb54cf99efb",
                "StandardName": "U.K.G",
                "DivisionId": "ac2b6419-a01b-46e1-a5e3-23e84cb04af9",
                "DivisionName": "A"
            },
            {
                "StandardId": "b2e5f4a8-2abb-4362-a968-e96d9bce3dce",
                "StandardName": "I",
                "DivisionId": "895f06a5-1bf6-488b-87e4-2f8497e27680",
                "DivisionName": "B"
            },
            {
                "StandardId": "08fafc51-3ebe-4e9c-8351-af06dc15ca16",
                "StandardName": "II",
                "DivisionId": "ac2b6419-a01b-46e1-a5e3-23e84cb04af9",
                "DivisionName": "A"
            },
            {
                "StandardId": "08fafc51-3ebe-4e9c-8351-af06dc15ca16",
                "StandardName": "II",
                "DivisionId": "895f06a5-1bf6-488b-87e4-2f8497e27680",
                "DivisionName": "B"
            },
            {
                "StandardId": "ed61b45d-a779-4d65-8330-745853396980",
                "StandardName": "III",
                "DivisionId": "ac2b6419-a01b-46e1-a5e3-23e84cb04af9",
                "DivisionName": "A"
            },
            {
                "StandardId": "ed61b45d-a779-4d65-8330-745853396980",
                "StandardName": "III",
                "DivisionId": "895f06a5-1bf6-488b-87e4-2f8497e27680",
                "DivisionName": "B"
            },
            {
                "StandardId": "96e671e2-c1fe-4879-b3bc-88c721292edf",
                "StandardName": "IV",
                "DivisionId": "895f06a5-1bf6-488b-87e4-2f8497e27680",
                "DivisionName": "B"
            }
        ]
    }
}

我使用ArrayList在2个不同的微调器中加载标准名称和分区名称。如果您看到图像,则所有标准和分区都会加载到相应的微调器中。现在,如果我从标准微调器中选择IV,则划分微调器应加载AB,如果选择标准I,则只会加载B应该加载到除法旋转器中。我怎样才能做到这一点?

Standard List Division List

2 个答案:

答案 0 :(得分:0)

您应该在第一个微调器中侦听更改并相应地更改第二个微调器。

您可以这样做:

standardNameSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        List<String> values = findDivisionNamesBasedOnStandardName(parent.getItemAtPosition(position));
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                view.getContext(), android.R.layout.simple_spinner_item, android.R.id.text1);
        divisionNameSpinner.setAdapter(adapter);
    }
});

findDivisionNamesBasedOnStandardName 方法中,您必须将有关参数的相关分部名称作为选定的标准名称返回。

<强> 编辑:

对于映射部分,创建一个类:

public class Subject {
    String standardId;
    String standardName;

    String divisionId;
    String divisionName;
}

然后将所有结果放入List<Subject>。然后你可以迭代这个列表然后findDivisionNamesBasedOnStandardName将是这样的:

public List<String> findDivisionNamesBasedOnStandardName(String standardName) {
    List<String> result = new ArrayList<>();
    for (Subject subject : mSubjects) {
        if (subject.standardName == standardName) {
            result.add(subject.divisionName);
        }
    }
    return result;
}

其中mSubjects是您从JSON响应中提取的所有主题的列表。

答案 1 :(得分:0)

确定。它几乎帮助了我。

  • 制作两个旋转器。

  • 之后,setOnItemSelectedListener将两者都用。

  • 制作两种方法 - filterOne(int selectedId)filterTwo(int selectedId)

伪代码:

Spinner first,second;
SpinnerAdapter firstAdapter, secondAdapter;

filterOne(int id) {
    secondAdapter = new CustomSpinnerAdapter(id);
    second.setAdapter(secondAdapter);
}

filerTwo(int id) {
    firstAdapter = new CustomSpinnerAdapter(id);
    first.setAdapter(firstAdapter);
}

ArrayList<Date> dates;
CustomSpinnerAdapter(int id) {
    dates = new ArrayList<Date>();
    // on click from first spinner. set data to second.
    if(id == 0)  {
        dates.add(new Date(2015, 06, 10));
        dates.add(new Date(2015, 06, 11));
    } else if(id == 1) {
    // on click from second spinner. set data to first.
        dates.add(new Date(2015, 06, 10));
    }
}