为什么我的微调器没有使用我的arrayList项更新?

时间:2013-10-02 03:57:49

标签: android arraylist spinner loading

我有当前的方法,sActivity有一个我想要使用的公共arrayList。 我无法理解,我的微调器没有任何值。这个微调器在另一个活动中,而不是我向arraylist添加项目的活动。所以也许我必须intent.putExtra("arrayL", savedColors)所以我必须将arraylist发送到这个活动...我不知道只是在猜测。任何帮助深表感谢。我已经将一些ColorSaver对象硬编码到arrayList中,所以我知道它中有值。

private void addColorNames()
{
    Spinner colorsSpinner = (Spinner) findViewById(R.id.colorsSpinner);

    ArrayAdapter<ColorSaver> dataAdapter 
        = new ArrayAdapter<ColorSaver>
        (RecallActivity.this, android.R.layout.simple_spinner_item, sActivity.savedColors);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    colorsSpinner.setAdapter(dataAdapter);

}//End addColorNames()

public class RecallActivity extends Activity {


ArrayList<String> namesArray = new ArrayList<String>();
SaveActivity sActivity = new SaveActivity();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recall);
    // Show the Up button in the action bar.
    setupActionBar();
    final Intent intent1 = new Intent(this, MainActivity.class);
    final Spinner colorList = (Spinner) findViewById(R.id.colorsSpinner);
    Button grabButton = (Button) findViewById(R.id.grabButton);

    //Load the spinner with the saved colors
    addColorNames();


    //namesArray.addAll(sActivity.nameArray);
    //colorsSpinner.add;
    grabButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            //ColorSaver saver = new ColorSaver(colorName, redcolor, greencolor, bluecolor);
            //savedColors.add(saver);
            //startActivity(intent1);
            ColorSaver selectedItem = (ColorSaver) colorList.getSelectedItem();

            int redValue, greenValue, blueValue;
            String name;
            redValue = selectedItem.getRedValue();
            greenValue = selectedItem.getGreenValue();
            blueValue = selectedItem.getBlueValue();
            name = selectedItem.getColorName();
            intent1.putExtra("savedRValue", redValue);
            intent1.putExtra("savedGValue", greenValue);
            intent1.putExtra("savedBValue", blueValue);
            intent1.putExtra("savedName", name);
            startActivity(intent1);


        }//END onClick
    });
}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.recall, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}// END onOptionsItemSelected(MenuItem item)


public void addColorNames()
{
    Spinner colorsSpinner = (Spinner) findViewById(R.id.colorsSpinner);

    ArrayAdapter<ColorSaver> dataAdapter 
        = new ArrayAdapter<ColorSaver>
        (RecallActivity.this, android.R.layout.simple_spinner_item, sActivity.savedColors);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    colorsSpinner.setAdapter(dataAdapter);

}//End addColorNames()

} // END CLASS

我将项目保存到数组的类是

public class SaveActivity extends Activity {

private static final String TAG = "Save Activity";
public ArrayList<ColorSaver> savedColors = new ArrayList<ColorSaver>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_save);
    // Show the Up button in the action bar.
    setupActionBar();

    final Intent intent1 = new Intent(this, MainActivity.class);
    Button saveButton = (Button) findViewById(R.id.saveButton1);
    final EditText nameField = (EditText) findViewById(R.id.colorNameField);
    final Intent intent = new Intent();

    //Making sure the savedColors arrayList has something in it.
    ColorSaver temp = new ColorSaver("TestColor", 180, 80, 255);
    savedColors.add(temp);


    saveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click

            int redcolor, greencolor, bluecolor;
            redcolor = intent.getIntExtra("RedValue", 255);
            greencolor = intent.getIntExtra("GreenValue", 255);
            bluecolor = intent.getIntExtra("BlueValue", 255);
            String colorName = nameField.getText().toString();

            ColorSaver saver = new ColorSaver(colorName, redcolor, greencolor, bluecolor);
            savedColors.add(saver);
            Log.i(TAG, savedColors.get(savedColors.size()-1).getColorName());
            startActivity(intent1);

        }
    });

}//END OnCreate()

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.save, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

} // END CLASS

1 个答案:

答案 0 :(得分:1)

当您从其他活动访问时,来自sActivity的ArrayList可能是null

检查它是null还是内部有值,或者只使用Intent将数组传递给Spinner的活动。 (使用Intent的额外数据更安全

相关问题