向适配器添加新项时 ArrayAdapter 崩溃
ArrayAdapter crashes when adding new items to adapter
我遇到一个问题,当我尝试向我的微调器动态添加项目时,应用程序崩溃了。所以首先我从 Strings.xml
添加一个数组,我的 R.array.restrictions
包含 16 个项目,因此我将每个键插入 16,然后将它添加到下一个位置。然后我从 firebase 加载每个项目并将其添加到适配器,然后我设置适配器,所以在我看来它应该可以工作。任何想法为什么会导致崩溃?说:
UnsupportedOperationException at
java.util.AbstractList.add(AbstractList.java:148)
谢谢。
public void startSpinner(){
//built in Profiles
spinner = (Spinner) findViewById(R.id.spinnerProfiles);
adapter = ArrayAdapter.createFromResource(this, R.array.restrictions, android.R.layout.simple_spinner_item);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
map = (Map<String, Object>) dataSnapshot.child("users").child(userID).getValue();
ArrayList<String> array = new ArrayList<>();
int x = 16;
for (Map.Entry<String,Object> entry : map.entrySet()) {
// key contains Profile Name
String key = entry.getKey();
adapter.insert(key, x);
x++;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Auto Generated Method
}
});
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
也许问题在于您正在更改来自资源的数组。这可能是因为您正在使用的列表不是您创建的。
您可以尝试以下方法:
ArrayList<CharSequence> array = new ArrayList<CharSequence>(Arrays.asList(context.getResources().getTextArray(R.array.restrictions)));
adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, array);
我遇到一个问题,当我尝试向我的微调器动态添加项目时,应用程序崩溃了。所以首先我从 Strings.xml
添加一个数组,我的 R.array.restrictions
包含 16 个项目,因此我将每个键插入 16,然后将它添加到下一个位置。然后我从 firebase 加载每个项目并将其添加到适配器,然后我设置适配器,所以在我看来它应该可以工作。任何想法为什么会导致崩溃?说:
UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148)
谢谢。
public void startSpinner(){
//built in Profiles
spinner = (Spinner) findViewById(R.id.spinnerProfiles);
adapter = ArrayAdapter.createFromResource(this, R.array.restrictions, android.R.layout.simple_spinner_item);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
map = (Map<String, Object>) dataSnapshot.child("users").child(userID).getValue();
ArrayList<String> array = new ArrayList<>();
int x = 16;
for (Map.Entry<String,Object> entry : map.entrySet()) {
// key contains Profile Name
String key = entry.getKey();
adapter.insert(key, x);
x++;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Auto Generated Method
}
});
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
也许问题在于您正在更改来自资源的数组。这可能是因为您正在使用的列表不是您创建的。 您可以尝试以下方法:
ArrayList<CharSequence> array = new ArrayList<CharSequence>(Arrays.asList(context.getResources().getTextArray(R.array.restrictions)));
adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, array);