如何在 Android 应用程序中使用 string.xml 文件值

发布于 2024-12-21 15:29:04 字数 2659 浏览 0 评论 0原文

这是代码。

string.xml

<?xml version="1.0" encoding="utf-8" ?> 
- <resources>
- <string-array name="countries_array">
  <item>Bahrain</item> 
  <item>Bangladesh</item> 
  <item>Barbados</item> 
  <item>Belarus</item> 
  <item>Belgium</item> 
  <item>Belize</item> 
  <item>Benin</item> 
  </string-array>
  </resources>

Layout

list_item.xml

<?xml version="1.0" encoding="utf-8" ?> 
  <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" />

main.xml

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 
  </LinearLayout>

.java 文件

package examples.com;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      String[] countries = getResources().getStringArray(R.array.countries_array);
      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
        }
      });
    }
} 

这是生成 xml 文件中某些名称的动态列表的代码。它将名称列表作为 xml 文件的输入并动态显示。从 xml 文件获取数据时,我收到未定义符号的错误:

error is in this line

      String[] countries = getResources().getStringArray(R.array.countries_array);

cant find symbol array in R.

countries_array is in string.xml file. If this method of accessing it is wrong then how to access it?
Thanx in advance.

This is the code..

string.xml

<?xml version="1.0" encoding="utf-8" ?> 
- <resources>
- <string-array name="countries_array">
  <item>Bahrain</item> 
  <item>Bangladesh</item> 
  <item>Barbados</item> 
  <item>Belarus</item> 
  <item>Belgium</item> 
  <item>Belize</item> 
  <item>Benin</item> 
  </string-array>
  </resources>

Layout

list_item.xml

<?xml version="1.0" encoding="utf-8" ?> 
  <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" />

main.xml

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 
  </LinearLayout>

.java file

package examples.com;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      String[] countries = getResources().getStringArray(R.array.countries_array);
      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
        }
      });
    }
} 

This is code of generating a dyanamic list of some names as in xml file. It takes list of names as input from xml file and display it dyanamicaly. While fetching data from xml file i am getting an error of undefined symbol as-

error is in this line

      String[] countries = getResources().getStringArray(R.array.countries_array);

cant find symbol array in R.

countries_array is in string.xml file. If this method of accessing it is wrong then how to access it?
Thanx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-12-28 15:29:04

将数组声明放入 res/values 目录中名为 arrays.xml 的文件中。当您尝试输入 R.array.countries_array 作为资源标识符时,系统会在此处查找。换句话说,

res/values/arrays.xml

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
  <string-array name="countries_array">
    <item>Bahrain</item> 
    <item>Bangladesh</item> 
    <item>Barbados</item> 
    <item>Belarus</item> 
    <item>Belgium</item> 
    <item>Belize</item> 
    <item>Benin</item> 
    </string-array>
</resources>

HTH!

Put your array declaration into a file titled arrays.xml in the res/values directory. This is where the system is looking when you try to type R.array.countries_array as a resource identifier. In other words,

res/values/arrays.xml

<?xml version="1.0" encoding="utf-8" ?> 
<resources>
  <string-array name="countries_array">
    <item>Bahrain</item> 
    <item>Bangladesh</item> 
    <item>Barbados</item> 
    <item>Belarus</item> 
    <item>Belgium</item> 
    <item>Belize</item> 
    <item>Benin</item> 
    </string-array>
</resources>

HTH!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文