Android - 可以使用xml数组向ExpandableList插入唯一的父项和唯一子项吗?

时间:2016-10-20 09:24:25

标签: android arrays xml expandablelistview expandablelistadapter

所以我是android的初学者,在阅读了有关ExpandableList在线的教程之后,我在我的应用程序中创建了一个FAQ列表。

但是,我想知道是否可以通过读取xml数组向ExpandableList添加唯一的父项和唯一子项?例如,在xml中有一个问题数组和答案数组,答案将以问题列表的相同顺序出现,如下所示:



<string-array name="faq_question">
        <item>Q1</item>
        <item>Q2</item>
  ...
</string-array>

<string-array name="faq_answer">
        <item>A1</item>
        <item>A2</item>
  ...
</string-array>
&#13;
&#13;
&#13;

目前,我必须手动将答案字符串放到各自的问题中:

ExpandableListDataPump.java

&#13;
&#13;
public class ExpandableListDataPump extends AppCompatActivity {

    public static HashMap<String, List<String>> getData() {
        HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();

        String q1 = MainActivity.getContext().getResources().getString(R.string.faq_q1);
        String q2 = MainActivity.getContext().getResources().getString(R.string.faq_q2);
        String q3 = MainActivity.getContext().getResources().getString(R.string.faq_q3);
        String a1 = MainActivity.getContext().getResources().getString(R.string.faq_a1);
        String a2 = MainActivity.getContext().getResources().getString(R.string.faq_a2);
        String a3 = MainActivity.getContext().getResources().getString(R.string.faq_a3);

        List<String> qt3 = new ArrayList<String>();
        qt3.add(a3);
        List<String> qt2 = new ArrayList<String>();
        qt2.add(a2);
        List<String> qt1 = new ArrayList<String>();
        qt1.add(a1);

        expandableListDetail.put(q1, qt1);
        expandableListDetail.put(q2, qt2);
        expandableListDetail.put(q3, qt3);
        return expandableListDetail;
    }
}
&#13;
&#13;
&#13;

适配器 - ExpandableListAdapter.java

&#13;
&#13;
public class ExpandableListAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {

    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;

    public ExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getRealChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.activity_help_faq_content, null);
        }
        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.child_list);
        expandedListTextView.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getRealChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.activity_help_faq_title, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.header_list);
        listTitleTextView.setText(listTitle);

        ViewFlipper flip = (ViewFlipper) convertView.findViewById(R.id.arrow_flipper);
        if (isExpanded) {
            flip.setDisplayedChild(1);
        } else {
            flip.setDisplayedChild(0);
        }
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return false;
    }
}
&#13;
&#13;
&#13;

活动 - HelpFaqActivity.java

&#13;
&#13;
public class HelpFaqActivity extends AppCompatActivity {

    AnimatedExpandableListView expandableListView;
    ExpandableListAdapter expandableListAdapter;
    List<String> expandableListTitle;
    HashMap<String, List<String>> expandableListDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_faq);
        setTitle(R.string.help_faq);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        expandableListView = (AnimatedExpandableListView) findViewById(R.id.lvExp);
        expandableListDetail = ExpandableListDataPump.getData();
        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
        expandableListAdapter = new ExpandableListAdapter(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                if (expandableListView.isGroupExpanded(groupPosition)) {
                    expandableListView.collapseGroupWithAnimation(groupPosition);
                    Log.d("MainActivity", "Collapsing");
                } else {
                    expandableListView.expandGroupWithAnimation(groupPosition);
                    Log.d("MainActivity", "Expanding");
                }
                return true;
            }

        });

        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                return false;
            }
        });
    }
}
&#13;
&#13;
&#13;

如果有任何方法可以实现这一点,或者更好的方法来做同样的结果,请告诉我。感谢。

1 个答案:

答案 0 :(得分:0)

从主要活动发送上下文到您的班级:

expandableListDetail = ExpandableListDataPump.getData(getApplicationContext());

并将您的ExpandableListDataPump更改为:

public class ExpandableListDataPump extends AppCompatActivity {

    public static HashMap<String, List<String>> getData(Context context) {
        HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();

        String[] questions = context.getResources().getStringArray(R.array.faq_question);
        String[] answers = context.getResources().getStringArray(R.array.faq_answer);

        //add error checking in case the length of questions and answers do not match
        for (int i = 0; i < questions.length; i++)  {
            List<String> answerList = new ArrayList<>();
            answerList.add(answers[i]);
            expandableListDetail.put(questions[i], answerList);
        }
        return expandableListDetail;
    }
}