可扩展列表视图中的Hashmap顺序因设备而异

时间:2015-09-15 23:27:29

标签: switch-statement expandablelistview linkedhashmap dataprovider

现在,我正在创建一个应用程序,它应该在可扩展列表视图中显示课程列表,当用户点击子元素时,它会打开一个包含课程详细信息的新活动,一切正常,但是,当我通过nexus测试应用程序时,课程的顺序是相同的,我把它放在hashmap和switch case语句中,但是当我通过我的银河系测试它而不是两个时,它发生了灾难,列表中的命令被扰乱了所以点击事件搞砸了什么时候应该打开x的课程细节,它打开y等等,我该怎么办?

这是主要的

public class Courses extends ActionBarActivity {


    HashMap<String,List<String>> Courses_castegory;

    List<String> Courses_list;

    ExpandableListView expandableListView;

    MyCourseAdapter myCourseAdapter;

    Intent detaledActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courses);

        expandableListView = (ExpandableListView) findViewById(R.id.simple_expandable_list);
        Courses_castegory = CourseProvider.getInfo();
        Courses_list=new ArrayList<>(Courses_castegory.keySet());//get all the keys from the hashmap

        myCourseAdapter = new MyCourseAdapter(this,Courses_castegory,Courses_list);

        expandableListView.setAdapter(myCourseAdapter);


        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getBaseContext(), Courses_list.get(groupPosition) + " is collapsed", Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                //Toast.makeText(getBaseContext(),
                //Courses_castegory.get(Courses_list.get(groupPosition)).get(childPosition)+" from "+Courses_list.get(groupPosition)+" is selected",Toast.LENGTH_SHORT).show();

                switch(groupPosition) {
                    case 0:
                        switch (childPosition) {
                            case 0:
                                detaledActivity = new Intent(Courses.this, DetailedCouse.class);
                                startActivity(detaledActivity);
                                break;

                        }
                }

                return true;
            }
        });

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_courses, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }





}

这是课程提供者

public class CourseProvider {

public static HashMap<String,List<String>> getInfo(){
    HashMap<String,List<String>>CourseDetails = new HashMap<String,List<String>>();

    List<String> CourseType1 = new ArrayList<>();
    CourseType1.add("English For Work");


    List<String> CourseType2= new ArrayList<>();
    CourseType2.add("General English For Work");
    CourseType2.add("Intensive English For Work");

    List<String> CourseType3 = new ArrayList<>();
    CourseType3.add("English Academic Year");
    CourseType3.add("English Academic Semester");

    List<String> CourseType4 = new ArrayList<>();
    CourseType4.add("IELTS Exam Preparation");
    CourseType4.add("TOEFL Exam Preparation");
    CourseType4.add("GMAT Exam Preparation");
    CourseType4.add("GRE Exam Preparation");

    List<String> CourseType5 = new ArrayList<>();
    CourseType5.add("Young Learners");

    List<String> CourseType6 = new ArrayList<>();
    CourseType6.add("General English Language");
    CourseType6.add("Intensive English Language");
    CourseType6.add("30+");

    CourseDetails.put("Learn English in Your Teacher's Home",CourseType1);
    CourseDetails.put("English For Work",CourseType2);
    CourseDetails.put("Academic Semester / Year",CourseType3);
    CourseDetails.put("Exam Preparation Courses",CourseType4);
    CourseDetails.put("Young Learners",CourseType5);
    CourseDetails.put("Flexible Courses",CourseType6);

    return CourseDetails;
}}

这是课程适应器

public class MyCourseAdapter extends BaseExpandableListAdapter {

private Context context;
private HashMap<String,List<String>>Courses_Category;
private List<String> Course_List ;


public MyCourseAdapter(Context context ,HashMap<String,List<String>>Courses_Category,List<String> Course_List) {

    this.context=context;
    this.Courses_Category=Courses_Category;
    this.Course_List=Course_List;


}



@Override
public int getGroupCount() {
    return Course_List.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return Courses_Category.get(Course_List.get(groupPosition)).size();

}

@Override
public Object getGroup(int groupPosition) {
    return Course_List.get(groupPosition);
}

@Override
public Object getChild(int parent, int child) {
    return Courses_Category.get(Course_List.get(parent)).get(child);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int parent, int child) {
    return child;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String group_title = (String) getGroup(groupPosition);
    if (convertView==null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.parent_courses,parent,false);

    }


    TextView parent_textview = (TextView) convertView.findViewById(R.id.parent_text_view);
    parent_textview.setTypeface(null, Typeface.BOLD);
    parent_textview.setText(group_title);

    return convertView;

}

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

    String child_title = (String) getChild(parentPosition,childPosition);

    if (convertView==null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView=inflater.inflate(R.layout.child_courses,parent,false);

    }

    TextView child_textview = (TextView) convertView.findViewById(R.id.child_text_view);
    child_textview.setText(child_title);

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public void onGroupCollapsed(int groupPosition){
    super.onGroupCollapsed(groupPosition);
}
public void onGroupExpanded(int groupPosition){
    super.onGroupExpanded(groupPosition);
}}
//

1 个答案:

答案 0 :(得分:0)

使用LinkedHashMap代替HashMap

完成