views:

115

answers:

1

Hi,

Need some advice from you experts out there. I've just started with Android programming and while I finally got what I want my "Hello World" to do, I feel as if I'm bludgening my way through rather than grasping concepts.

I created three EditText boxes. Below those I created three Spinners. Pick a number in the Spinner and it will show up in the corresponding EditText box. Spinner 1 correlates to EditText 1, Spinner 2 to EditText 2 and Spinner 3 to EditText 3. I had a hell of a time getting this to work. I do have some cleaning up to do.

In my startup class I:

    EditText [] pick_nums = new EditText[3];
    pick_nums[0] = (EditText) findViewById(R.id.r1c1);
    pick_nums[1] = (EditText) findViewById(R.id.r1c2);
    pick_nums[2] = (EditText) findViewById(R.id.r1c3);

    Spinner test1 = (Spinner) findViewById(R.id.spin_pick_num1);
    Spinner test2 = (Spinner) findViewById(R.id.spin_pick_num2);
    Spinner test3 = (Spinner) findViewById(R.id.spin_pick_num3);

//I created a class to initialize the spinners.

Context g = getApplicationContext();
    Initialize_stuff spin1 = new Initialize_stuff(test1, g);
    Initialize_stuff spin2 = new Initialize_stuff(test2, g);
    Initialize_stuff spin3 = new Initialize_stuff(test3, g);

//I created a class to put the number in the corresponding EditText box. I wanted to avoid having the user press a button to do the populating and once three numbers are in I'll do something with them without having to press a button.

Handle_Picks hp = new Handle_Picks(g, pick_nums);

//Here's where I think it get kludgy and awkward. To make Handle_Picks work correctly I created a method to set the context, an array of EditTexts, and the reference to the class Handle_Picks hp.

Spinner_Listener listen = new Spinner_Listener();    
listen.set_context(g, pick_nums, hp);
test1.setOnItemSelectedListener(listen);
test2.setOnItemSelectedListener(listen);
test3.setOnItemSelectedListener(listen);

//In the Spinner_Listener class

public class Spinner_Listener implements OnItemSelectedListener

static Handle_Picks hp1;  //set in a method
private String num;
String v;
String spin_id;
int vv;

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id)
{
    //To make this work I had to find the name of the spinner that was
    //used so I could correlate it with the right EditText

     vv = parent.getId();
     v = parent.getResources().getResourceName(vv);
     spin_id = parent.getResources().getResourceEntryName(vv);
     setNum(parent.getItemAtPosition(pos).toString());
}

public void setNum(String s)
{
    num = s;
    hp1.handle_nums(spin_id, num);
}

//finally in class Handle_Picks I:

public class Handle_Picks extends Activity
{
  static Context theContext;
  EditText [] et;
  String spin_num;

// constructor  
Handle_Picks(Context f, EditText[] array)
{
    theContext = f;
    et = array;
}


public void handle_nums(String id, String aNumber)
{
    spin_num = id;
    if (spin_num.equalsIgnoreCase("spin_pick_num1"))
        et[0].setText(aNumber);
    else if (spin_num.equalsIgnoreCase("spin_pick_num2"))
        et[1].setText(aNumber);
    else if (spin_num.equalsIgnoreCase("spin_pick_num3"))
        et[2].setText(aNumber);
}

Is there a better way to pass or reference resources around various classes? Having to pass the Context and and array of EditText to class Handle_Picks before I could use Handle_Picks from class Spinner_Listener doesn't seem right. Seems kind of dangerous.

What's the better way?

A: 

Ummmm...well...very little of what you have here do I like much. Tackling the easy stuff:

  • To get the value of a Spinner, call getSelectedItem(). There is no sense in your messing around with the resources to populate your EditText based off of the Spinner value. In your Spinner_Listener's onItemSelected() method, your Spinner is known as parent (Spinner inherits from AdapterView, and getSelectedItem() is a method on AdapterView).

  • Don't call getApplicationContext(), particularly for UI-related stuff. Your Activity is a Context -- just use it.

  • Never implement a constructor on an Activity (e.g., Handle_Picks).

  • Never create an instance of an Activity directly (e.g., new Handle_Picks()).

Given all the above, you should not need Handle_Picks at all.

CommonsWare