Dưới đây là đoạn code mà tôi đã thực hiện để lấy item mảng chuỗi:Android - lấy chuỗi mảng từ nguồn
String[] menuArray;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
menuArray = getResources().getStringArray(R.array.menu);
for(int i = 0; i < menuArray.length; i++)
{
Button b = new Button(this);
b.setText(menuArray[i]);
ll.addView(b);
}
this.setContentView(sv);
}
Đây là file strings.xml:
<string-array name="menu">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
Tuy nhiên, R.array.menu có vấn đề này để biên dịch: Kể từ ADT 14, trường tài nguyên không thể được sử dụng làm trường hợp chuyển đổi. Gọi bản sửa lỗi này để biết thêm thông tin.
Yeah, tôi có thể cung cấp cho bạn ảnh chụp từ giả lập nếu bạn muốn xem. Tôi chỉ bỏ qua việc sử dụng biến ** x ** cùng với thử khối catch theo định nghĩa ** count() ** không được cung cấp. –
Bạn có đang sử dụng các nút chuyển với id tài nguyên không? kiểm tra [This] (http://stackoverflow.com/questions/8387777/what-is-migrate-android-code) –
tôi giải quyết nó d .. nó chỉ là vì tôi di chuyển ra app_name từ strings.xml LOL –