1

I am trying to create an onDisimiss listener to my app as for now this is what i have

The activity

public class listview_test extends Activity {
ListView list;
String[] web = {
        "Google Plus",
        "Twitter",
        "Windows",
} ;
Integer[] imageId = {
        R.drawable.ic_launcher,
        R.drawable.icon,
        R.drawable.ic_launcher,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_test);
    final CustomList adapter = new
            CustomList(listview_test.this, web, imageId);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(listview_test.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
        }
    });
    SwipeDismissListViewTouchListener touchListener =
            new SwipeDismissListViewTouchListener(
                    list,
                    new SwipeDismissListViewTouchListener.DismissCallbacks() {
                        @Override
                        public boolean canDismiss(int position) {
                            return true;
                        }

                        @Override
                        public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                adapter.remove(adapter.getItem(position));
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }
            );
    list.setOnTouchListener(touchListener);
    // Setting this scroll listener is required to ensure that during ListView scrolling,
    // we don't look for swipes.
    list.setOnScrollListener(touchListener.makeScrollListener());
}
}

And the custom adapter

public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
                  String[] web, Integer[] imageId) {
    super(context, R.layout.weather_item, web);
    this.context = context;
    this.web = web;
    this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.weather_item, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.city);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.info_image);
    txtTitle.setText(web[position]);
    imageView.setImageResource(imageId[position]);
    return rowView;
}

For some reason every time i am swiping an item to dismiss it force closes and gives me this exception

    java.lang.UnsupportedOperationException

In this line

adapter.remove(adapter.getItem(position));

1 Answer 1

3
java.lang.UnsupportedOperationException

is thrown when you back an Adapter by an array or non-modifiable List. Since you cannot change their size, deleting is impossible.

Instead, modify your Adapter so it accepts a List instead of an array and make sure that the List you pass off is fully flexible.

Something like

List <String> web = new ArrayList <String> ();
web.add ("Google Plus");
web.add ("Twitter");
//etc.

is enough to ensure a flexible list.

This means that your CustomList adapter should also call up to the superclass constructor that also accepts in a List, which in this case is

public ArrayAdapter (Context context, int resource, List<T> objects)

For more information refer to the ArrayAdapter documentation.

Sign up to request clarification or add additional context in comments.

2 Comments

so instead of an "ArrayAdapter" i should use ListAdapter?
@user3068269 no, ListAdapter is an interface implemented by ArrayAdapter. Continue to use ArrayAdapter but change the super call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.