Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly overwrite methods of SpinnerAdapter

Tags:

android

Im using a SpinnerAdapter to display a Spinner. For that i defined a class which implements SpinnerAdapter. Unfortunately I don't know how to override some methods of SpinnerAdapter:

@Override
public View getDropDownView(int position, View arg1, ViewGroup arg2) {

}


@Override
public int getItemViewType(int arg0) {

}

@Override
public boolean hasStableIds() {

}

@Override
public void registerDataSetObserver(DataSetObserver arg0) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

}

Does anybody know what i have to code in each of these methods?

like image 407
RoflcoptrException Avatar asked Jun 18 '10 18:06

RoflcoptrException


3 Answers

Thanks for the answers, but nevertheless I want to share with you the solution I used after searching more references:

Instead that just implementing SpinnerAdapter in my Adapter, I extend BaseAdapter and implement SpinnerAdapter:

private class ListAdapter extends BaseAdapter implements SpinnerAdapter {

        @Override
        public int getCount() {
            return allLists.size();
        }

        @Override
        public Object getItem(int position) {
            return allLists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            TextView text = new TextView(lexs);
            text.setText(allLists.get(position).getName());
            return text;
        }

    }

Then it isn't necessary to override all this strange methods such as isEmpty(), registerDataObserver(), etc.

And if necessry one can still override getDropDownView(...)

Additionally using this solution, one can call adapter.notifyDatasetChanged() which isn't as easy if the adapter just implements SpinnerAdapter and doesn't extend BaseAdapter.

like image 141
RoflcoptrException Avatar answered Nov 01 '22 20:11

RoflcoptrException


The most important method if getDropDownView(). This creates a View instance to display the data at the selected ID. arg1 is the convert View, an existing View which can be modified and returned by the function so as to not need to create a whole new View each time the user selects a data index. arg2 is the parent ViewGroup into which the created (or converted) View should be placed.

The other methods are inherited from the Adapter class, and their implementation can only really be understood in that context. Their usage is documented here.

like image 2
tlayton Avatar answered Nov 01 '22 21:11

tlayton


Look at the sources of BaseAdapter. You just need to add these lines to your custom ListAdapter or SpinnerAdapter implementation.

private final DataSetObservable mDataSetObservable = new DataSetObservable();

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    mDataSetObservable.registerObserver(observer);
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    mDataSetObservable.unregisterObserver(observer);
}

/**
 * Notifies the attached observers that the underlying data has been changed
 * and any View reflecting the data set should refresh itself.
 */
public void notifyDataSetChanged() {
    mDataSetObservable.notifyChanged();
}
like image 1
Yaroslav Mytkalyk Avatar answered Nov 01 '22 21:11

Yaroslav Mytkalyk