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.
*
* CRITICAL make sure that if sets or cards are null, that we don't blow up
*
* Created : 28/02/12 12:10 PM MST
*
* Modified : $Date$ UTC
*
* Revision : $Revision$
*
* @author Trenton D. Adams
*/
public class EditActivity extends ICBaseActivity
implements AdapterView.OnItemSelectedListener
{
private Spinner spinner;
private ListView listView;
private List sets;
/**
* Sets up the spinner with an array adapter
*
* 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(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 cards;
public LayoutAdapter(final Context context, final List 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 cards)
{
this.cards = cards;
notifyDataSetChanged();
}
}
}