SelectVariations.java :  » Log » gymrat » com » emuporium » gymrat » android » Android Open Source

Android Open Source » Log » gymrat 
gymrat » com » emuporium » gymrat » android » SelectVariations.java
package com.emuporium.gymrat.android;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.emuporium.gymrat.android.data.Variation;

public class SelectVariations extends ListActivity {
  SQLiteDatabase db;
  ArrayList<Variation> variationList = null; // list of available variations
  // from db
  ArrayList<Variation> selected;
  int exercise = -1;
  SortedSet<Integer> selectedVariations = null; // list of currently checked

  // variations

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("yo", "create");
    setContentView(R.layout.variation_select);

    variationList = new ArrayList<Variation>();
    selectedVariations = new TreeSet<Integer>();
    Button submit = (Button) findViewById(R.id.submit_variations);

    db = DbUtils.getStaticDb();
    

    exercise = getIntent().getIntExtra(
        "com.emuporium.gymrat.android.ExerciseChoice", 0);

    // Log.e("Yo", "Received:" + exercise);

    Cursor c = db
        .rawQuery(
            "Select variations._id, variation from variations join exercisevariations on variationid=variations._id\r\n"
                + "where exercisevariations.exerciseid = ?",
            new String[] { Integer.toString(exercise) });
    startManagingCursor(c);

    ListAdapter adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_checked, c,
        new String[] { "variation" }, new int[] { android.R.id.text1 });

    setListAdapter(adapter);

    while (c.moveToNext()) {
      Variation v = new Variation();
      v.setId(c.getInt(0));
      v.setName(c.getString(1));

      variationList.add(v);
    }
    
//    CheckBox cb = (CheckBox) getListAdapter().getView(1, null, null);
//    getListView().setItemChecked(1, true);
    
//    CheckedTextView check = (CheckedTextView) v;
//    check.toggle();

    submit.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Intent intent = new Intent(SelectVariations.this,
            SelectEquipment.class);
        ArrayList<Integer> a = new ArrayList<Integer>();
        a.addAll(selectedVariations);
        intent.putIntegerArrayListExtra(
            "com.emuporium.gymrat.android.Variations", a);
        intent.putExtra("com.emuporium.gymrat.android.ExerciseChoice",
            exercise);
        startActivityForResult(intent, 0);
      }
    });

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 2) {
      setResult(resultCode, data);
      finish();
    }

  }

  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate if the process is
    // killed and restarted.

    // ArrayList<Integer> a = new ArrayList<Integer>();
    // a.addAll(selectedVariations);
    // selected = new ArrayList<Variation>();
    //
    // savedInstanceState.putIntegerArrayList("Selected", a);

    super.onSaveInstanceState(savedInstanceState);
  }

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    ArrayList<Integer> s = new ArrayList<Integer>();
    // s = savedInstanceState.getIntegerArrayList("Selected");
    // if (s != null)
    // selectedVariations.addAll(savedInstanceState
    // .getIntegerArrayList("Selected"));

    // ListView l = (ListView) findViewById(R.layout.variation_select);
    // l.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    // l.setItemChecked(0, true);
    // CheckedTextView ctv = (CheckedTextView) l.get;
    // ctv.toggle();
  }

  @Override
  protected void onListItemClick(ListView list, View v, int pos, long row) {
    // super.onListItemClick(list, v, pos, id);
//    list.setItemChecked(1, true);
    
    // Log.e("Group", variationList.get(pos).toString() + ":" + pos);
    if (!selectedVariations.add(variationList.get(pos).getId()))
      selectedVariations.remove(variationList.get(pos).getId());

    CheckedTextView check = (CheckedTextView) v;
    check.toggle();

    StringBuilder sb = new StringBuilder();

    // for(int i = 0; i < selectedVariations.size(); i++){
    // sb.append(selectedVariations.

    Iterator iter = selectedVariations.iterator();

    while (iter.hasNext()) {
      Integer i = (Integer) iter.next();
      sb.append(i);
    }
    // Log.e("Selected", "Current: " + sb);

    // Intent intent = new
    // Intent(SelectVariations.this,SelectEquipment.class);
    // intent.putExtra("com.emuporium.gymrat.android.MuscleGroup",
    // muscleList.get(pos));
    // intent.putExtra("com.emuporium.gymrat.android.Name",
    // intent.getStringExtra("com.emuporium.gymrat.android.Name"));
    // startActivityForResult(intent, PICK_EXERCISE_REQUEST);

  }

  @Override
  public void onResume() {
    super.onResume();
    if (!db.isOpen())
      db = DbUtils.getStaticDb();
  }

  @Override
  public void onPause() {
    super.onPause();
//    db.close();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.