更新时ArrayAdapter UnsupportedOperationException为什么?

时间:2013-01-30 08:29:58

标签: android android-fragments android-arrayadapter

升级ArrayAdapter时出现UnsupportedOperationException。 这是我的ArrayAdapter:

public class UserArrayAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;

public UserArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_user, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_user, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    if (s.equals("WindowsMobile")) {
        imageView.setImageResource(R.drawable.search_marker);
    } else if (s.equals("iOS")) {
        imageView.setImageResource(R.drawable.androidmarker);
    } else if (s.equals("Blackberry")) {
        imageView.setImageResource(R.drawable.balloon_disclosure);
    } else {
        imageView.setImageResource(R.drawable.androidmarker);
    }

    return rowView;
}
}

我在我的ListFragment类中使用此方法调用它:

    String[] user = 
        new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};
.
.
.
private void setListUser(){

    if(getListAdapter()==null){
        // init adapter
        adapter=new UserArrayAdapter(getActivity(),
                user);
        setListAdapter(adapter);  
    }
    else{           
        adapter.add("try");
        adapter.notifyDataSetChanged();
    }           

}

在创建时我可以正确查看我的列表,但是当我更新我的列表时,我明白了吗?

01-30 09:25:54.439: E/AndroidRuntime(22634): FATAL EXCEPTION: main
01-30 09:25:54.439: E/AndroidRuntime(22634): java.lang.UnsupportedOperationException
01-30 09:25:54.439: E/AndroidRuntime(22634):    at java.util.AbstractList.add(AbstractList.java:404)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at java.util.AbstractList.add(AbstractList.java:425)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.map.manager.AddUser.setListUser(AddUser.java:79)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.map.manager.AddUser.SearchByEmailOrName(AddUser.java:98)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.map.manager.AddUser.access$0(AddUser.java:84)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.map.manager.AddUser$1.onQueryTextSubmit(AddUser.java:110)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.widget.SearchView.onSubmitQuery(SearchView.java:1100)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.widget.SearchView.access$900(SearchView.java:91)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.widget.SearchView$8.onEditorAction(SearchView.java:1078)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.widget.TextView.onEditorAction(TextView.java:3790)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:104)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.os.Looper.loop(Looper.java:137)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at android.app.ActivityThread.main(ActivityThread.java:4575)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at java.lang.reflect.Method.invokeNative(Native Method)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at java.lang.reflect.Method.invoke(Method.java:511)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-30 09:25:54.439: E/AndroidRuntime(22634):    at dalvik.system.NativeStart.main(Native Method)

谢谢!

1 个答案:

答案 0 :(得分:-1)

public View getView(int position,View convertView,ViewGroup parent){ 改为

public View getView(int position,View rowView,ViewGroup parent){

View rowView = inflater.inflate(R.layout.list_user, parent, false);

更改为

rowView = inflater.inflate(R.layout.list_user,parent,false);

只需更改这两行

即可