All pastes #2122751 Raw Edit

Stuff

public java v1 · immutable
#2122751 ·published 2012-02-29 06:35 UTC
rendered paste body
package com.example.flashcard;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.*;import com.example.flashcard.db.Card;import com.example.flashcard.db.Set;import java.util.List;/** * This activity's primary purposes is for editing card sets. * <p/> * CRITICAL make sure that if sets or cards are null, that we don't blow up * <p/> * Created :  28/02/12 12:10 PM MST * <p/> * Modified : $Date$ UTC * <p/> * Revision : $Revision$ * * @author Trenton D. Adams */public class EditActivity extends ICBaseActivity    implements AdapterView.OnItemSelectedListener{    private Spinner spinner;    private ListView listView;    private List<Set> sets;    /**     * Sets up the spinner with an array adapter     * <p/>     * IMPORTANT use the ChooseSetDSO, so that we can notify the set of changes     * when they attempt to delete all cards, and also choose to delete the set     * at the same time.     *     * @param savedInstanceState ignored     */    public void onCreate(final Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.editsets);        sets = flashDb.getSets();        final ArrayAdapter<? extends Set> adapter = new ArrayAdapter<Set>(this,            android.R.layout.simple_spinner_item, sets);        adapter.registerDataSetObserver(new ChooseSetDSO(flashDb, sets));        spinner = (Spinner) findViewById(R.id.choose_set_spinner);        spinner.setAdapter(adapter);        spinner.setOnItemSelectedListener(this);        listView = (ListView) findViewById(R.id.card_list);//        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);        final LayoutAdapter layoutAdapter = new LayoutAdapter(this, sets.get(            0).getCards());        listView.setAdapter(layoutAdapter);    }    public void onItemSelected(final AdapterView<?> parent,        final View view, final int position, final long id)    {        final LayoutAdapter layoutAdapter =            (LayoutAdapter) listView.getAdapter();        layoutAdapter.setCards(sets.get(position).getCards());    }    public void onNothingSelected(final AdapterView<?> parent)    {    }    /**     * A layout adapter for the list of cards currently being displayed in the     * EditActivity.     */    @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")    public class LayoutAdapter extends BaseAdapter    {        private LayoutInflater inflater;        private Context context;        private List<Card> cards;        public LayoutAdapter(final Context context, final List<Card> cards)        {            this.cards = cards;            inflater = LayoutInflater.from(context);            this.context = context;        }        /**         * Retrieves the view for a single list item.  This can be entirely         * custom.         */        public View getView(final int position, final View convertView,            final ViewGroup parent)        {            View itemLayout = convertView; // the view containing the item            final TextView itemText;            final CheckBox itemCheckbox;            if (itemLayout == null)            {                itemLayout = inflater.inflate(R.layout.editcards, parent,                    false);            }            itemText = (TextView) itemLayout.findViewById(R.id.card_title);            itemCheckbox =                (CheckBox) itemLayout.findViewById(R.id.card_checked);            itemText.setText(cards.get(position).toString());            // CRITICAL  Okay, this seems to be very inefficient.  We're            // going to create a new click listener for EVERY element?            ((ListView) parent).setOnItemClickListener(                new AdapterView.OnItemClickListener()                {                    public void onItemClick(final AdapterView<?> parent,                        final View view, final int position, final long id)                    {                        Toast.makeText(context, cards.get(position).toString()                            + (itemCheckbox.isChecked() ? " - X" : ""),                            Toast.LENGTH_LONG).show();                    }                });            return itemLayout;        }        public int getCount()        {            return cards.size();        }        public Object getItem(final int position)        {            return cards.get(position);        }        public long getItemId(final int position)        {            return cards.get(position).getId();        }        public void setCards(final List<Card> cards)        {            this.cards = cards;            notifyDataSetChanged();        }    }}