0

升级 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)

谢谢!

4

1 回答 1

-1

public View getView(int position, View convertView, ViewGroup parent) { change to

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

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

change to

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

Just change these 2 lines

于 2013-01-30T08:45:18.510 回答