在ListView Android中一次解析两个不同的XML文件

时间:2019-03-06 16:27:58

标签: java xml-parsing xmlpullparser

我正在android项目中处理xml,我是初学者。我也有使用不同语言的相同xml

<sura index="1" name="الفاتحة">
    <aya index="1" text="الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ 
    الرَّحِيمِ" />
    <aya index="2" text="الرَّحْمَنِ الرَّحِيمِ" />
    <aya index="3" text="مَالِكِ يَوْمِ الدِّينِ" />
    <aya index="4" text="إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ" />
    <aya index="5" text="اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ" />
    <aya index="6" text="صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ" />
</sura>

这是阿拉伯语

<sura index="1" name="الفاتحة">
    <aya index="1" text="In the name of Allah, most benevolent, ever-merciful."/>
    <aya index="2" text="ALL PRAISE BE to Allah, Lord of all the worlds,"/>
    <aya index="3" text="Most beneficent, ever-merciful,"/>
    <aya index="4" text="King of the Day of Judgement."/>
    <aya index="5" text="You alone we worship, and to You alone turn for help."/>
    <aya index="6" text="Guide us (O Lord) to the path that is straight,"/>
    <aya index="7" text="The path of those You have blessed, Not of those who have earned Your anger, nor those who have gone astray."/>
</sura>

这个用英语。

我已成功使用此代码完成了阿拉伯语aya的解析

private List<String> getAllAyaFromSuraIndex(String suraIndex) throws XmlPullParserException, IOException {
     list = new ArrayList<>();

    XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);
    while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
        if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("sura")) {
            String index = parser.getAttributeValue(0);

            // Match given sure index
            if (index.equals(suraIndex)) {
                // Move to first aya tag inside matched sura index
                parser.next();

                // This loop will exit when it reaches sura end tag </sura>
                while (parser.getName().equals("aya")) {
                    if (parser.getEventType() == XmlPullParser.START_TAG) {
                        list.add(parser.getAttributeValue(null,"index") + ".\n" + parser.getAttributeValue(null,"text"));
                    }

                    // Move to next aya tag
                    parser.next();
                }

                break;
            }
        }

        parser.next();
    }

    return list;
}

我有一个listview,其中有两个textview,一个用于阿拉伯语,一个用于英语。我尝试了一切,但没有得到结果。

1 个答案:

答案 0 :(得分:2)

在开发多语言应用程序时,在android中处理翻译的最佳方法是在locale类别中定义内容。让我解释一下你

第一步 创建带有提到的语言环境名称的目录 values-(语言),例如values-ar

MyProject/
          res/
              values-es/
                        strings.xml
              values-ar/
                        strings.xml

values-es / strings.xml

 <resources>
     <string name="ayah1">ALL PRAISE BE to Allah, Lord of all the worlds,.</string>
     <string name="ayah2">Most beneficent, ever-merciful,</string>
 </resources>

values-ar / strings.xml

 <resources>
     <string name="ayah1">الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ</string>
     <string name="ayah2">الرَّحْمَنِ الرَّحِيمِ</string>
 </resources>

第二步现在在视图文件中将这些字符串用作

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.javapapers.multilingualapp.MainActivity">

    <TextView
        android:id="@+id/string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ayah1"                 //here the values being used
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</android.support.constraint.ConstraintLayout>

第三步也是最后一步,现在,只要您想更改语言,就可以使用以下功能更改语言环境

public void setLocale(String localeName) {
        if (!localeName.equals(currentLanguage)) {
            myLocale = new Locale(localeName);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = myLocale;
            res.updateConfiguration(conf, dm);
            Intent refresh = new Intent(this, MainActivity.class);
            refresh.putExtra(currentLang, localeName);
            startActivity(refresh);
        } else {
            Toast.makeText(MainActivity.this, "Language already selected!", Toast.LENGTH_SHORT).show();
        }
    }